在PHP中生成日期字符串

时间:2012-01-10 18:16:24

标签: php datetime date

有没有办法在两个时间戳之间生成所有日期字符串? 例如,我有一个最早的时间戳和最新的1303887600(2011-04-27T00:00:00-07:00), 1325318400(2011-12-31T00:00:00-08:00)时间戳,所以我希望从2011-04-272011-12-31

如果使用for循环将时间戳转换为日期,则可以在夏令时之间切换。

3 个答案:

答案 0 :(得分:1)

尝试这样的事情

$temp_date = $start_date;
While($temp_date <= $end_date)
{
    print date("d-m-Y", strtotime($temp_date))
    $temp_date = $temp_date + 1 day;
}

答案 1 :(得分:-1)

for($i=1303887600;$i<=1325318400;$i++) echo date(DATE_ATOM,$i) . "<br />";

答案 2 :(得分:-1)

$startDate = DateTime::createFromFormat("Y/m/d","2010/12/24",new DateTimeZone("Europe/London")); 
$endDate = DateTime::createFromFormat("Y/m/d","2012/01/05",new DateTimeZone("Europe/London")); 

$periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule 
$endDate->add( $periodInterval );
$period = new DatePeriod( $startDate, $periodInterval, $endDate ); 

foreach($period as $date){ 
   echo $date->format("Y-m-d") , PHP_EOL; 
}