• support@answerspoint.com

How to add 4 hours to time in PHP/MySQL

2569

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.

3Answer


0
date($format, strtotime("$date + 4 hours"));
  • answered 8 years ago
  • G John

0

What about:

date( "Y-m-d H:i:s", strtotime( "2009-08-09 23:44:22" )+4*60*60 )

or even

date( "Y-m-d H:i:s", strtotime( "2009-08-09 23:44:22 + 4 hours" ) )

Might need a bit of error checking, but should solve your problem.

  • answered 8 years ago
  • Gul Hafiz

0

I have used this method:

$format = Y-m-d; //you can use any format if you want
$date = $row['date']; // from mysql_fetch_array
$date2 = date($format, strtotime("$date + 4 week"));
echo $date;
 
  • answered 8 years ago
  • G John

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 2569 times
  • active 8 years ago

Best Rated Questions