0

1 時間間隔で 2 つの日付間の日付の配列を生成したいと考えています。

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
$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 に答える