Page 1 of 1

increment date one by one

Posted: Fri Dec 25, 2009 2:11 pm
by dheeraj
Hello, i have a date which i m getting from database in the format of "dd/mm/YYYY", now the thing i want to do is to increment that date by one continuously....
e.g.
$date = "25/12/2009";

then it should be
26/12/2009
27/12/2009...
and so on..

Re: increment date one by one

Posted: Fri Dec 25, 2009 3:05 pm
by manohoo
This will get you started:

Code: Select all

 
$date = "25/12/2009";
 
$a =( explode ("/",$date));
$day = $a[0];
$mo = $a[1];
$yr = $a[2];
 
//mktime(hour,minute,second,month,day,year,is_dst) 
echo(date("d-m-Y",mktime(0,0,0,$mo,$day,$yr))."<br />");
$day = $day+1;
echo(date("d-m-Y",mktime(0,0,0,$mo,$day,$yr))."<br />");
 
Will output:
25-12-2009
26-12-2009

Re: increment date one by one

Posted: Fri Dec 25, 2009 3:23 pm
by Eran
Also:

Code: Select all

echo date('d/m/Y',strtotime($date . " +1 day")); 
You can replace +1 with a variable that is incremented in a loop. I'm not sure if it will work with your format though.

Re: increment date one by one

Posted: Fri Dec 25, 2009 3:28 pm
by manohoo
pytrin, your code:

Code: Select all

 
$date = "25/12/2009";
echo date('d/m/Y',strtotime($date . " +1 day"));
 
yields this:
01/01/1970
... which is incorrect.

Re: increment date one by one

Posted: Fri Dec 25, 2009 3:31 pm
by Eran
As I said, I wasn't sure if it will work with his format. It works with the standard "Y-m-d" (SQL format)

Re: increment date one by one

Posted: Fri Dec 25, 2009 4:21 pm
by MichaelR

Code: Select all

echo date('d/m/Y', strtotime(str_replace('/', '-', $date) . " +1 day"));
strtotime() doesn't like forward slashes.

Re: increment date one by one

Posted: Sat Dec 26, 2009 12:35 am
by dheeraj
hey 1st one & last one nicely worked, thanx guys...
& thanx to all of one who replied....