Jump to content

Date loop


Canman2005

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.