1

Is there a way to generate all date strings between two timestamps? For example, I have a earliest timestamp and a lastest timestamp of 1303887600(2011-04-27T00:00:00-07:00), 1325318400(2011-12-31T00:00:00-08:00), so I want from 2011-04-27 to 2011-12-31.

If using a for loop to convert timestamp to date, there is a switch between daylight saving time.

DrXCheng
  • 3,992
  • 11
  • 49
  • 72
  • Yes sure! But please try it yourself before you ask thx. – noob Jan 10 '12 at 18:18
  • 3
    possible duplicate of [PHP: Return all dates between two dates in an array](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – JJJ Jan 10 '12 at 18:21
  • 1
    possible duplicate of [PHP - Is there a simple way to loop between two dates and fill in missing values?](http://stackoverflow.com/questions/8755650/php-is-there-a-simple-way-to-loop-between-two-dates-and-fill-in-missing-values) – Mark Baker Jan 10 '12 at 19:23

3 Answers3

1

Try something like this

$temp_date = $start_date;
While($temp_date <= $end_date)
{
    print date("d-m-Y", strtotime($temp_date))
    $temp_date = $temp_date + 1 day;
}
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
-1
for($i=1303887600;$i<=1325318400;$i++) echo date(DATE_ATOM,$i) . "<br />";
j08691
  • 204,283
  • 31
  • 260
  • 272
-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; 
} 
Mark Baker
  • 209,507
  • 32
  • 346
  • 385