phpbar.de logo

Mailinglisten-Archive

[php] get weekdays

[php] get weekdays

Michael Borchers list at tridemail.de
Mon Jan 8 10:33:24 CET 2007


folgende funktion, die ich irgendwo gefunden habe, lief wunderbar, bis zum 
jahreswechsel.
er erkennt schon, dass wir uns in der 2.woche des jahres befinden, aber der 
timestamp wird nicht korrekt für heute berechnet.

laut mysql ist der timestamp für heute, den 08.01.2007:
1168210800

doch laut funktion: 1167649200

wo ist der fehler?



function get_weekdays($week,$year)
{
 $week_dates = array();

 // Get timestamp of first week of the year
 $first_day = mktime(12,0,0,1,1,$year);
 $first_week = date("W",$first_day);

 if ($first_week > 1) {
    $first_day = strtotime("+1 week",$first_day); // skip to next if year 
does not begin with week 1
 }

 // Get timestamp of the week
 $timestamp = strtotime("+$week week",$first_day);


 // Adjust to Monday of that week
 $what_day = date("w",$timestamp); // I wanted to do "N" but only version 
4.3.9 is installed :-(


 if ($what_day==0) {
    // actually Sunday, last day of the week. FIX;
    $timestamp = strtotime("-6 days",$timestamp);
 } elseif ($what_day > 1) {
    $what_day--;
    $timestamp = strtotime("-$what_day days",$timestamp);
 }

 $week_dates[1] = date("Y-m-d",$timestamp); // Monday
 $week_dates[2] = date("Y-m-d",strtotime("+1 day",$timestamp)); // Tuesday
 $week_dates[3] = date("Y-m-d",strtotime("+2 day",$timestamp)); // Wednesday
 $week_dates[4] = date("Y-m-d",strtotime("+3 day",$timestamp)); // Thursday
 $week_dates[5] = date("Y-m-d",strtotime("+4 day",$timestamp)); // Friday
 #   $week_dates[6] = date("Y-m-d",strtotime("+5 day",$timestamp)); // 
Saturday
 #   $week_dates[7] = date("Y-m-d",strtotime("+6 day",$timestamp)); // 
Sunday
 return($week_dates);
}


php::bar PHP Wiki   -   Listenarchive