Yika Posted January 26, 2007 Share Posted January 26, 2007 Hello All!I have a problem of understanding how I should get the dates in between to set dates. For example, I want to get all the dates listed from Feb 21 to March 08, or maybe Dec 29 to Jan 04, so on and so forth. Im just not sure how I can do this logically with PHP.I have no coding yet, because I have NO idea how to approach this.. so perhaps someone can help me out, or maybe there is already a script for this?? Years wouldn't matter a bit.Im thinking I'll need to have Counters, and If statements, but please help T-T[u][/u] Link to comment https://forums.phpfreaks.com/topic/35855-php-date-operations-getting-all-the-dates-between-jan-20-to-feb-3/ Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 [code]$feb21 = mktime(0, 0, 0, 2, 21, date("Y"));$mar08 = mktime(0, 0, 0, 3, 8, date("Y"));while($time=$feb21; $time<=$mar08; $time = $time+(60*60*24)){ print date("F jS, Y", $time);}[/code](not tested) Link to comment https://forums.phpfreaks.com/topic/35855-php-date-operations-getting-all-the-dates-between-jan-20-to-feb-3/#findComment-169974 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 You want to use the function [url=http://www.php.net/strtotime]strtotime()[/url] to get the UNIX time stamp for the start and end of the range. Use a "for" loop, starting at the start date time stamp, going until it's greater than the ending time stamp, incrementing by 86400 (the number of seconds in a day).Something like this:[code]<?phpfunction list_dates($start,$end) { $ret = array(); $sts = strtotime($start); $ets = strtotime($end); if ($ets < $sts) return (false); for($i=$sts;$i<=$ets;$i+=86400) $ret[] = $i; return($ret);}$dates = list_dates('January 20, 2007','February 3,2007');if (is_array($dates)) foreach ($dates as $dt) echo date('l, F j, Y',$dt) . '<br>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35855-php-date-operations-getting-all-the-dates-between-jan-20-to-feb-3/#findComment-169976 Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 Isn't that what I did only using strtotime instead of mktime, and longer? :-P Link to comment https://forums.phpfreaks.com/topic/35855-php-date-operations-getting-all-the-dates-between-jan-20-to-feb-3/#findComment-169980 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 jesirose: don't you want a "for" loop, not the "while" statement?We posted at about the same time. Yes, it's longer, but I made it a generic function.Ken Link to comment https://forums.phpfreaks.com/topic/35855-php-date-operations-getting-all-the-dates-between-jan-20-to-feb-3/#findComment-169981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.