Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 4 hours to time in PHP/MySQL

I'm working on a blog migration from a custom built blog to Wordpress. One of the fields that Wordpress is looking for in the database is a date/time stamp set to GMT, which is 4 hours ahead of our time. So I basically need to take our date/time stamp (in YYYY-MM-DD HH:MM:SS format), and add four hours to it. I was looking at the MySQL command "ADDTIME", but I think that only works on selects, not on inserts.

I had worked up a script that exploded the date in to parts, and added 4 hours to the time, but the ensuing logic that would be required to check for when 4 hours pushes in to the next day/month/year seemed a little excessive.

like image 505
TWLATL Avatar asked Nov 02 '09 14:11

TWLATL


2 Answers

date($format, strtotime("$date + 4 hours"));
like image 118
moo Avatar answered Oct 22 '22 16:10

moo


There's nothing that prevents ADDTIME() from being used in an INSERT OR UPDATE, but DATE_ADD() is probably what will work best:

INSERT INTO table_name
SET my_datetime = DATE_ADD('2009-11-01 19:30:00', INTERVAL 4 HOURS),
...other insert columns here... ;
like image 33
artlung Avatar answered Oct 22 '22 17:10

artlung