mikefrederick Posted March 4, 2008 Share Posted March 4, 2008 Thanks ahead of time: If I have two dates , say February 28, 2008 and March 28, 2008, how can I echo every date between them? I supposed I could calculate the difference between the two and do for($day=0, $day<=$difference, $day++) and then do a day+$i type of thing, any other suggestions? Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/ Share on other sites More sharing options...
craygo Posted March 4, 2008 Share Posted March 4, 2008 <?php $date1 = strtotime("February 28, 2008"); $date2 = strtotime("March 28, 2008"); for($i=date1; $i<=$date2; $i+=86400){ echo date("Y-m-d", $i); } ?> Ray Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483206 Share on other sites More sharing options...
tomasd Posted March 4, 2008 Share Posted March 4, 2008 <?php $date1 = strtotime("February 28, 2008"); $date2 = strtotime("March 28, 2008"); for($i=date1; $i<=$date2; $i+=86400){ echo date("Y-m-d", $i); } ?> Ray I'm trying to do same thing and having some problems... if I run your script I'm getting days counted since 1970... [tomas@dummy array]$ php test.php | wc -l 13967 I tried same using <?php $start_day = "01"; $start_month = "01"; $start_year = "2007"; $end_day = "10"; $end_month = "01"; $end_year = "2007"; $start_date = date('Ymd', mktime(0, 0, 0, $start_month, $start_day, $start_year)); $end_date = date('Ymd', mktime(0, 0, 0, $end_month, $end_day, $end_year)); for ($i = $start_date; $i < $end_date; $i++) { echo "$i \n"; $newdate = date('c', $i); echo "$newdate \n"; } ?> http://www.phpfreaks.com/forums/index.php/topic,185565.msg830818.html#msg830818 Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483217 Share on other sites More sharing options...
craygo Posted March 4, 2008 Share Posted March 4, 2008 I run the script above I get 2008-02-28 2008-02-29 2008-03-01 2008-03-02 2008-03-03 2008-03-04 2008-03-05 2008-03-06 2008-03-07 2008-03-08 2008-03-09 2008-03-10 2008-03-11 2008-03-12 2008-03-13 2008-03-14 2008-03-15 2008-03-16 2008-03-17 2008-03-18 2008-03-19 2008-03-20 2008-03-21 2008-03-22 2008-03-23 2008-03-24 2008-03-25 2008-03-26 2008-03-27 Ray Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483228 Share on other sites More sharing options...
obsidian Posted March 4, 2008 Share Posted March 4, 2008 How about this: <?php $seed = "February 28, 2008"; $start = strtotime($seed); $end = strtotime("March 28, 2008"); $i = 0; while ($start <= $end) { $start = strtotime("{$seed} +{$i} days"); echo date('F j, Y', $start) . "<br />\n"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483232 Share on other sites More sharing options...
tomasd Posted March 4, 2008 Share Posted March 4, 2008 This worked for me! <?php $start_day = "01"; $start_month = "02"; $start_year = "2008"; $end_day = "01"; $end_month = "03"; $end_year = "2008"; $start_date = mktime(0, 0, 0, $start_month, $start_day, $start_year); $end_date = mktime(0, 0, 0, $end_month, $end_day, $end_year); for ($i = $start_date; $i < $end_date; $i+=86400) { $newdate = date('Y-m-d', $i); echo "$newdate \n"; } ?> [tomas@dummy array]$ php test1.php 2008-02-01 2008-02-02 2008-02-03 2008-02-04 2008-02-05 2008-02-06 2008-02-07 2008-02-08 2008-02-09 2008-02-10 2008-02-11 2008-02-12 2008-02-13 2008-02-14 2008-02-15 2008-02-16 2008-02-17 2008-02-18 2008-02-19 2008-02-20 2008-02-21 2008-02-22 2008-02-23 2008-02-24 2008-02-25 2008-02-26 2008-02-27 2008-02-28 2008-02-29 leap year works too Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483234 Share on other sites More sharing options...
tomasd Posted March 4, 2008 Share Posted March 4, 2008 How about this: <?php $seed = "February 28, 2008"; $start = strtotime($seed); $end = strtotime("March 28, 2008"); $i = 0; while ($start <= $end) { $start = strtotime("{$seed} +{$i} days"); echo date('F j, Y', $start) . "<br />\n"; $i++; } ?> I tried following <?php $seed = "2008-02-01"; $start = strtotime($seed); $end = strtotime("2008-03-01"); $i = 0; while ($start < $end) { $start = strtotime("{$seed} +{$i} days"); echo date('Y-m-d', $start) . "\n"; $i++; } ?> for some reason first day of march is also included... [tomas@dummy array]$ php test3.php 2008-02-01 2008-02-02 2008-02-03 2008-02-04 2008-02-05 2008-02-06 2008-02-07 2008-02-08 2008-02-09 2008-02-10 2008-02-11 2008-02-12 2008-02-13 2008-02-14 2008-02-15 2008-02-16 2008-02-17 2008-02-18 2008-02-19 2008-02-20 2008-02-21 2008-02-22 2008-02-23 2008-02-24 2008-02-25 2008-02-26 2008-02-27 2008-02-28 2008-02-29 2008-03-01 this works well if I'm putting $end = strtotime("2008-03-00"), but that's not really a date... Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483238 Share on other sites More sharing options...
tomasd Posted March 4, 2008 Share Posted March 4, 2008 sorry wrong topic Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483250 Share on other sites More sharing options...
obsidian Posted March 4, 2008 Share Posted March 4, 2008 for some reason first day of march is also included... Ugh... my stupidity on that one. Try this as a fix: <?php $seed = "February 28, 2008"; $start = strtotime($seed); $end = strtotime("March 28, 2008"); $i = 0; while ($start < $end) { echo date('F j, Y', $start) . "<br />\n"; $i++; $start = strtotime("{$seed} +{$i} days"); } ?> Link to comment https://forums.phpfreaks.com/topic/94351-php-all-dates-between-2-dates/#findComment-483259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.