我想在两个日期之间生成一个日期数组,间隔为 1 小时。
Inital date: 01-01-2013 00:00:00
Final date: 02-01-2013 00:00:00
前任。结果:
[01-01-2013 00:00:00, 01-01-2013 01:00:00, 01-01-2013 02:00:00, (...), 02-01-2013 00:00:00]
尝试这个
$dates = array();
$start = strtotime('01-01-2013 00:00:00');
$end = strtotime('02-01-2013 00:00:00');
for($i=$start;$i<$end;$i+=3600) {
$dates[] = date('Y-m-d H:i:s',$i);
}
$start = new DateTime('2013-07-01 00:00:00', new DateTimeZone('UTC'));
$interval = new DateInterval('PT1H');
$end = new DateTime('2013-07-03 00:00:00', new DateTimeZone('UTC'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$dateArray[] = $date->format('Y-m-d h:i:s');
}
var_dump($dateArray);
<?php
$start = '2013-01-01 00:00:00';
$end = '2013-01-02 00:00:00';
$dates = array();
$current = strtotime($start);
$offset = 0;
while ( $current < strtotime($end) ) {
$current = strtotime("$start +{$offset} hours");
$dates[] = date('d-m-Y H:i:s', $current);
$offset++;
}
print_r($dates);
你可以试试这个。
$start = mktime(0,0,0,1,1,2013);
$end = mktime(0,0,0,2,1,2013);
$inc = 60*60; // 1 hour
for ($x=$start; $x<=$end; $x+$inc ) {
$dates = date('d-m-Y H:i:s, $x);
}