Page 1 of 1

PHP mySQL newbie date calculation advice!

Posted: Wed Dec 16, 2009 11:03 am
by glow777
Could do with some help with the following problem.

I have extracted a date from a datetime field in a mysql database (yyy-mmm-ddd hh:mm:ss) held in f_date
and the result displays

echo $info['f_date'];

2009-11-14 12:00:00

however I would also like to display this date plus a variable amount of hours
eg
echo $info['f_date'] +5 hours;

2009-11-14 17:00:00

I have no idea how to go about this as it seems there is incompatability between PHP and mySQL formats


What I would like is a fix so I can complete the problem and a pointer to where to look so I can understand whats going off.

Thanks in advance

Re: PHP mySQL newbie date calculation advice!

Posted: Wed Dec 16, 2009 11:58 am
by AbraCadaver
date() and strtotime() are great:

Code: Select all

echo date("Y-m-d H:i:s", strtotime('+5 hours', strtotime($info['f_date'])));
//or
echo date("Y-m-d H:i:s", strtotime($info['f_date'])+18000);

Re: PHP mySQL newbie date calculation advice!

Posted: Wed Dec 16, 2009 1:30 pm
by glow777
thanks thats great and I now know where to investigate further, the only slight problem that isnt much of a problem is that one date is dashed and the other is slashed. Could use string replace

eg
2009-11-10 12:00:00 2009/11/11 22:00:00

thanks again
great forum

Re: PHP mySQL newbie date calculation advice!

Posted: Wed Dec 16, 2009 1:41 pm
by AbraCadaver
glow777 wrote:thanks thats great and I now know where to investigate further, the only slight problem that isnt much of a problem is that one date is dashed and the other is slashed. Could use string replace

eg
2009-11-10 12:00:00 2009/11/11 22:00:00

thanks again
great forum
My mistake. Just change the date('format'). I edited it in my post above.