0

我想在两个日期之间生成一个日期数组,间隔为 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]
4

4 回答 4

2

尝试这个

    $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);
    }
于 2013-07-05T10:21:46.987 回答
2
$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);
于 2013-07-05T10:29:23.180 回答
1
<?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);
于 2013-07-05T10:26:16.687 回答
0

你可以试试这个。

$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);
}
于 2013-07-05T10:26:05.490 回答