PHP mySQL newbie date calculation advice!

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
glow777
Forum Newbie
Posts: 2
Joined: Wed Dec 16, 2009 10:51 am

PHP mySQL newbie date calculation advice!

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP mySQL newbie date calculation advice!

Post 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);
Last edited by AbraCadaver on Wed Dec 16, 2009 1:40 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
glow777
Forum Newbie
Posts: 2
Joined: Wed Dec 16, 2009 10:51 am

Re: PHP mySQL newbie date calculation advice!

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP mySQL newbie date calculation advice!

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply