2009-10-28 36 views
3

我想要得到一个日期数组加上接下来的13个日期,以获得从给定日期开始的14天计划。PHP:从接下来的13天开始?

这里是我的功能:

$time = strtotime($s_row['schedule_start_date']); // 20091030 
$day = 60*60*24; 
for($i = 0; $i<14; $i++) 
{ 
    $the_time = $time+($day*$i); 
    $date = date('Y-m-d',$the_time); 
    array_push($dates,$date); 
} 

但似乎月份时切换到可重复的日期..

这就是我得到:

2009-10 -30 | 2009-10-31 | 2009-11-01 | 2009-11-01 | 2009-11-02 | 2009-11-03 | 2009-11-04 | 2009-11-05 | 2009-11-06 | 2009-11-07 | 2009-11-08 | 2009-11-09 | 2009-11-10 | 2009-11-11

请注意,2009-11-01重复。我无法弄清楚为什么?

我在做什么错?

谢谢!

回答

6

我会使用的strtotime

$start = strtotime($s_row['schedule_start_date']); 
$dates=array(); 
for($i = 1; $i<=14; $i++) 
{ 
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start))); 
} 
print_r($dates); 
+2

击败我23秒!!!!!!!!! – 2009-10-28 18:19:46

7

由于daylight saving time switch,你有相同的日期。第二天加24*60*60秒是不安全的,因为一年中的2天有更多/更少的秒数。当您从夏季切换到冬季时,您将增加1小时到一天。所以这一天将是25*60*60秒,这就是为什么它没有在你的代码中切换。

您可以通过mktime()来计算。例如:

## calculate seconds from epoch start for tomorrow 
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y")); 
## format result in the way you need 
$tomorrow_date = date("M-d-Y", $tomorrow_epoch); 

或完整版本代码:

$dates = array(); 
$now_year = date("Y"); 
$now_month = date("m"); 
$now_day = date("d"); 
for($i = 0; $i < 14; $i++) { 
    $next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year); 
    array_push(
     $dates, 
     date("Y-m-d", $next_day_epoch) 
    ); 
} 
+0

因为这使得,那我应该怎么做呢?谢谢。 – 2009-10-28 18:15:16

4

我建议是这样的:

for($i=1;$i<=14;$i++){ 
    echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days"))); 
}