我如何获得本周的前后日期

时间:2013-09-07 15:49:41

标签: php

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

我正在尝试获取当前月份的日前和日期。实际上日历已经开始工作,但与我的笔记本电脑的时间不一样。

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

$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

1 个答案:

答案 0 :(得分: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 ) );
?>