increment date one by one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

increment date one by one

Post 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..
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: increment date one by one

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: increment date one by one

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: increment date one by one

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: increment date one by one

Post 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)
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: increment date one by one

Post by MichaelR »

Code: Select all

echo date('d/m/Y', strtotime(str_replace('/', '-', $date) . " +1 day"));
strtotime() doesn't like forward slashes.
User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

Re: increment date one by one

Post by dheeraj »

hey 1st one & last one nicely worked, thanx guys...
& thanx to all of one who replied....
Post Reply