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
Share on other sites

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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.