Mega Code Archive

 
Categories / Php / Date Time
 

Function that returns an array with the 7 dates of the week that belong

to the supplied date. <? //This function receives a specific date and returns the days of the week //that the input day belongs to. The function returns an array with 7 dates //in this format : YYYY-mm-dd. function WeekDates($y,$m,$d){ $TimeStamp=mktime(0,0,0,$m,$d,$y); for($x=0; $x<=6; $x++){ $DateArray[$x] = date('Y-m-d',mktime(0,0,0,date('m',$TimeStamp),date ('d',$TimeStamp)-date('w',$TimeStamp)+$x,date('Y',$TimeStamp))); } return $DateArray; } //Example 1 $Array=WeekDates("2002","12","31"); For($i = 0 ; $i <= 6 ; $i++) { Echo "$i." . $Array[$i] . "<BR>"; } /* Output : 2002-12-29 2002-12-30 2002-12-31 2003-01-01 2003-01-02 2003-01-03 2003-01-04 */ //Example 2 $Array=WeekDates("2002","2","26"); For($i = 0 ; $i <= 6 ; $i++) { Echo "$i." . $Array[$i] . "<BR>"; } /* Output : 2002-02-24 2002-02-25 2002-02-26 2002-02-27 2002-02-28 2002-03-01 2002-03-02 */ //Example 3 $Array=WeekDates("2003","1","1"); For($i = 0 ; $i <= 6 ; $i++) { Echo "$i." . $Array[$i] . "<BR>"; } /* Output : 2002-12-29 2002-12-30 2002-12-31 2003-01-01 2003-01-02 2003-01-03 2003-01-04 */ ?>