PHP datetime每小时间隔

时间:2014-10-24 05:28:42

标签: php

我需要一个脚本来按小时间隔生成数千行日期时间。 (例如24/10/14 12:00:00,24/10/14 13:00:00等等)提前致谢。

$updatedDateTimeList = array();
$dateTimeList=array("2014-10-01 00:00:00","2014-11-01 00:00:00");
foreach ($dateTimeList as $dateTime)
{
    $time = substr($dateTime, 11);
    if (substr($time, 0, 2) < 24)
    {
        $updatedDateTimeList[] = substr($dateTime, 0, 11) + (substr($time, 0, 2) +              1) + substr($dateTime, 13);
    } else {
        $updatedDateTimeList[] = substr($dateTime, 0, 8) + (substr($dateTime, 8, 2) + 1                                                     ) + " 00" + substr($dateTime, 13);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在while循环中从开始日期开始设置startdate和enddate限制,每次传递可以添加1小时。请检查以下代码。

$updatedDateTimeList = array();
$startdate = date("Y-m-d H:i:s",strtotime ("2014-10-01 00:00:00"));
$enddate = date("Y-m-d H:i:s",strtotime ("2014-11-01 00:00:00"));
$updatedDateTimeList[] = $startdate; //store the first value
while ($startdate < $enddate)
{
  $startdate = strtotime($startdate)+3600; //add one hour to $startdate
  $startdate = date("Y-m-d H:i:s", $startdate); 
  $updatedDateTimeList[] = $startdate; // store to the array
}

 print_r($updatedDateTimeList);