PHP循环日期会导致错误

时间:2013-10-27 14:04:56

标签: php date loops

当日期为> = 2013-01-06

时,有人能告诉我为什么以下情况失败

这太奇怪了,当这个日期过去之后,剧本完美无缺,但之前的任何事情我都会得到白色的死亡画面!

<?php 

use Carbon\Carbon;

$startDate    = Carbon::createFromFormat('Y-m-d', '2013-01-06');
$current_week = Carbon::now()->timestamp;

/*
$startDate    = strtotime('2013-01-06');
$current_week = strtotime(date('Y-m-d'));
*/

$weeks        = array();
$w            = 0;

while($startDate < $current_week){
    $weeks[$w] = array(
        'monday' => $startDate->startofWeek()->format('d/m/Y'), 
        'sunday' => $startDate->endofWeek()->format('d/m/Y')
    );
    $w++;
    $startDate = $startDate->addDays(1); // Move it on to the following week
}

var_dump($weeks);

?>

请有人帮帮我吗?!

1 个答案:

答案 0 :(得分:1)

这个?我们将时间戳与时间戳进行比较,而不是带有时间戳的日期时间对象,我不知道Carbon类,这里$ startDate-&gt; timestamp必须替换为将carbon datetime对象转换为unix时间戳的方法。

<?php 

use Carbon\Carbon;

$startDate    = Carbon::createFromFormat('Y-m-d', '2013-01-06');
$startDateTimestamp = $startDate->timestamp;
$current_week = Carbon::now()->timestamp;

/*
$startDate    = strtotime('2013-01-06');
$current_week = strtotime(date('Y-m-d'));
*/

$weeks        = array();
$w            = 0;

while($startDateTimestamp < $current_week){
    $weeks[$w] = array(
        'monday' => $startDate->startofWeek()->format('d/m/Y'), 
        'sunday' => $startDate->endofWeek()->format('d/m/Y')
    );
    $w++;
    $startDate = $startDate->addDays(7); // Move it on to the following week
    $startDateTimestamp = $startDate->timestamp;
}

var_dump($weeks);

?>