• Home
  • Blog
  • Demos
  • About
  • ------------------
  • Articles
  • Csharp(59)
    • controls (4)
    • methods (5)
    • tutorials (50)
  • Html(1)
    • tutorials (1)
  • Java(10)
    • mobile (10)
  • Javascript(5)
    • tutorials (5)
  • Linux(3)
    • tutorial (3)
  • Math(10)
    • tutorials (10)
  • Php(15)
    • functions (1)
    • mysql (6)
    • tutorials (8)
  • Sql(23)
    • tutorials (23)
  • Vba(1)
    • tutorials (1)

MySql DateTime add

Written: 2009-09-15 23:57:56
Mood: Happy
Subject: programming
One problem I often have is that my web host is based some 8 hours away. This means that using NOW() on a datetime field in a mysql database sets the date 8 hours back for me.

I am aware that I'm not alone with this problem. A google search for php mysql add to datetime gives, currently, more than 400.000 hits.

The general way to solve this is to use the strtotime() function and add all the seconds then convert it back to a valid date format.

But there is an easier way if you just want to add the current datetime via MySql, like this:

$sql = "INSERT INTO table (datetime_column) VALUES (NOW() + INTERVAL 8 HOUR)";



It's as simple as that. Now we've just added 8 hours to the datetime.