php日历添加时区时显示错误的日期

时间:2018-08-18 05:15:09

标签: php calendar

我从https://www.emanueleferonato.com/2006/06/08/simple-php-calendar-with-any-day-offset/那里获得了php日历脚本

function calendar($year, $month, $day_offset = 0){ 
$days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
$months = array("january","february","march","april","may","june","july","august","september","october","november","december");
$day_offset = $day_offset % 7;
$start_day = gmmktime(0,0,0,$month,1,$year); 
$start_day_number = date("w",$start_day);
$days_in_month = date("t",$start_day);
$final_html .= "<table>\n<tr><td colspan = \"7\">".$months[$month-1]." $year</td></tr>\n";
for($x=0;$x<=6;$x++){
    $final_html .= "<td>".$days[($x+$day_offset)%7]."</td>";
}
$final_html .= "</tr>\n";
$blank_days = $start_day_number - $day_offset;
if($blank_days<0){$blank_days = 7-abs($blank_days);}
for($x=0;$x<$blank_days;$x++){
    $final_html .= "<td>x</td>";
}
for($x=1;$x<=$days_in_month;$x++){
    if(($x+$blank_days-1)%7==0){
        $final_html .= "</tr>\n<tr>";
    }
    $final_html .= "<td>$x</td>";
}
while((($days_in_month+$blank_days)%7)!=0){
    $final_html .= "<td>x</td>";
    $days_in_month++;
}
$final_html .= "</tr>\n</table>";
return($final_html);
}

它的工作完美,但如果我添加时区设置,则日期显示不正确。请帮忙。

date_default_timezone_set('America/Los_Angeles');

echo calendar(2018,8);

without timezone

with timezone

1 个答案:

答案 0 :(得分:0)

尝试更改:

start_day_number = date("w",$start_day);

收件人:

start_day_number = gmdate("w",$start_day);