Jump to content

Archived

This topic is now archived and is closed to further replies.

frustrated

inserting time using NOW() + time

Recommended Posts

Ok...I'm trying to insert the time into a database using the mysql NOW() function, but I wanted to offset the time to my actual time zone.

 

For instance, the server the site is hosted on is in California, but I live in New Hampshire. I want the time in the database to reflect New Hampshire time as opposed to California time...it's like a 3 hour difference. My inclination was to do something like this

 

$time_offset = /* Would equal the difference in time */
$sql = "INSERT INTO the_table (the_date) VALUES ((NOW() - $time_offset))";

 

Am I on the right track or is this not even possible with the NOW() function...or not possible at all?

 

Thanks for any help.

Share this post


Link to post
Share on other sites

If this is something you want to do on a more global basis, you should probably read [a href=\"http://dev.mysql.com/doc/refman/4.1/en/time-zone-support.html\" target=\"_blank\"]this refman page[/a]. For a one-time thing, though, you should probably be using something like "DATE_SUB(NOW(), INTERVAL $time_offset HOUR)".

 

Hope that helps.

Share this post


Link to post
Share on other sites

Nice, that helps a lot. I read the page you linked to. It would be nice to use that method but I don't think I have enough control over the database to do something like. I have no money and can only afford shared hosting :(

Share this post


Link to post
Share on other sites

What does money have to do with it? Even if you don't have permission to do it for the whole DB, you can always reset the time zone for all of your client sessions, which has the exact same effect! Does that make sense?

Share this post


Link to post
Share on other sites

exactly

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]#

 

Per-connection time zones. Each client that connects has its own time zone setting, given by the session time_zone variable. Initially this is the same as the global time_zone variable, but can be reset with this statement:

 

mysql> SET time_zone = timezone;

 

Just put this query in the connection.php of your script.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.