Canman2005 Posted March 4, 2007 Share Posted March 4, 2007 Hi all I have a loop code which take a date and loops it a number of times, I define the number of times with $duration, the code i'm using is; $duration = 3; $times = $duration; $x = 0; while ($x < $times) { print $day.$month.$year; print "<br>"; ++$x; } The above code produces something like 8/2/2007 8/2/2007 8/2/2007 How can I get it to take the date and for each time is loops, increase the date by 1 day, so the above looks like 8/2/2007 9/2/2007 10/2/2007 Is this possible? Any help would be ace Thanks in advance Dave Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/ Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 Try this, I think it'd work: <?php $duration = 3; $times = $duration; $x = 0; while ($x < $times) { $date = $day."/".$month."/".$year; echo date("d/m/Y", strtotime($date) + 24*60*60*$x); print "<br>"; ++$x; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199313 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 unfortunaltely for us Brits, strtotime doesn't have a clue about "d/m/y" format. try <?php $day = 8; $month = 2; $year = 2007; $startdate = mktime(0,0,0,$month,$day,$year); for ($i=0; $i<3; $i++) { echo date ('j/n/Y', strtotime("+$i days", $startdate)) . '<br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199323 Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 Does it use american dates? In that case, this line: $date = $day."/".$month."/".$year; Should become: $date = $month."/".$day."/".$year; Orio. Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199324 Share on other sites More sharing options...
Canman2005 Posted March 4, 2007 Author Share Posted March 4, 2007 Hi That works but produces the wrong date, for example, if you use Orio's code with the date 30/2/2006 then it returns 02/06/2008 03/06/2008 04/06/2008 if you use Barand's example with $day = 29; $month = 2; $year = 2007; then you get 1/3/2007 2/3/2007 3/3/2007 Strange! Can anyone help? Thanks Dave Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199326 Share on other sites More sharing options...
Canman2005 Posted March 4, 2007 Author Share Posted March 4, 2007 Nope, I use UK dates Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199329 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 if you use Barand's example with $day = 29; $month = 2; $year = 2007; then you get 1/3/2007 2/3/2007 3/3/2007 Strange! Can anyone help? Thanks Dave Thats because 29/2/2007 IS 1/3/2007 as it isn't a leap year Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199331 Share on other sites More sharing options...
Canman2005 Posted March 4, 2007 Author Share Posted March 4, 2007 Oh right, sorry, thanks. Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199333 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 Does it use american dates? This gives examples of acceptable formats <?php $dates = array ( '28/02/2007', '2/28/2007', '28-02-2007', '2-28-2007', '28-Feb-2007', '28 Feb 2007', 'Feb 28 2007' ); foreach ($dates as $str) { $ok = date ('Y-m-d', strtotime($str)) == '2007-02-28' ? 'OK' : 'x'; echo $str, ' --> ', $ok, '<br/>'; } ?> --> 28/02/2007 --> x 2/28/2007 --> OK 28-02-2007 --> x 2-28-2007 --> x 28-Feb-2007 --> OK 28 Feb 2007 --> OK Feb 28 2007 --> OK Link to comment https://forums.phpfreaks.com/topic/41143-date-loop/#findComment-199342 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.