1

大家好,我正在用我的日历构建。

我正在尝试为我的日历获取当前月份的前后日期。实际上日历已经在工作,但它与我笔记本电脑的时间不同。

我是否必须更改此代码中的某些内容?

$month = 9;
$year  = 2013;

// calculate the number of days in the current month 
$day_count = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// get number of days for the next month 
$pre_days = (6 - (date('w', mktime(0, 0, 0, $month, $day_count, $year))));

// get number of days after the month 
$post_days = date('w', mktime(0, 0, 0, $month, 1, $year));

然后我正在循环显示日期..

echo "<div id='cal_wrapper'>";
echo    "<div class='title_bar'>";
echo        "<div class='prev_month'></div>";
echo        "<div class='show_month'></div>";
echo        "<div class='next_month'></div>";
echo    "</div>"; // end title bar

echo    "<div class='week_days'>";
        foreach($weekDaysArr as $value)
        {
echo        "<div class='days_of_week'>".$value."</div>";

        }
echo   "<div class='clear'></div>";
echo    "</div>"; // end week days

        // Previous Month Filler
        if($pre_days != 0) {
            for($i = 1; $i <= $pre_days; $i++)
            {
echo            "<div class='non_cal_days'></div>";
            }
        }

        // Current day of the month filler
        if($day_count != 0 )
        {
            for($x = 1; $x <= $day_count; $x++)
            {
echo           "<div class='cal_days'>";
echo                "<div class='day_header'>".$x."</div>";
echo            "</div>";
            }
        }

        // Current day of the month filler
        if($post_days != 0 )
        {
            for($y = 1; $y <= $post_days; $y++)
            {
echo            "<div class='non_cal_days'></div>";
            }
        }

echo "</div>"; // end calendar wrapper
4

1 回答 1

0

将时区添加到您的脚本中:

<?php
    date_default_timezone_set('America/Los_Angeles');
    // or         date_default_timezone_set('Europe/Berlin');
    .
    .
    .

看看这里的时区:http: //php.net/manual/en/timezones.php

编辑

<?php
    $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
    echo $date->format('Y-m-d H:i:sP') . "<br />";

    $date = new DateTime();
    echo $date->format('U = Y-m-d H:i:s') .' <-- right now'; // this should return right now.
?>

更多信息http://php.net/manual/en/datetime.settimezone.php

对我来说,它是这样工作的

<?php
    $now = date ( 'Y-m-j G:i:s' );
    echo 'pre_days--> '. $pre_days = date ( 'Y-m-j G:i:s', strtotime ( '-6 day' . $now ) );
    echo '<br />'; 
    echo 'post_days--> '. $post_days = date ( 'Y-m-j G:i:s', strtotime ( '+1 year' . $now ) );
?>
于 2013-09-07T15:59:30.260 回答