Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through dates with PHP

Tags:

date

loops

php

I am trying to loop through dates with PHP. Currently my code gets stuck in a loop repeating 110307. I need the date format to be in yymmdd. Here is what I was trying to use:

<?php
    $check_date = '100227';
    $end_date = '100324';

    while($check_date != $end_date){
        $check_date = date("ymd", strtotime("+1 day", strtotime($check_date)));
        echo $check_date . '<br>';  
    }
?>
like image 587
shinjuo Avatar asked Mar 06 '11 06:03

shinjuo


1 Answers

Try using a unix timestamp and adding 86400 each time. That's gotta be faster than calling strtotime(). You can lookup timestamp conversions online.

<?php
    $check_date = 1267228800; // '2010-02-27';
    $end_date = 1269388800; // '2010-03-24';

    while($check_date != $end_date){
        $check_date += 86400;
        echo date("Ymd", $check_date) . '<br>';  
    }
?>
like image 61
Paul Schreiber Avatar answered Sep 21 '22 17:09

Paul Schreiber