PHP日期时间每小时间隔


PHP datetime hourly interval

我需要一个脚本来生成每小时间隔的千行日期时间。(例如,2014年10月24日12:00:00、2014年10日24日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);
    }
}

您可以在while循环中设置开始日期和结束日期限制,从开始日期开始,每次循环可以增加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);