Jump to content


Photo

inserting time using NOW() + time


  • Please log in to reply
5 replies to this topic

#1 frustrated

frustrated
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 15 January 2006 - 04:10 AM

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.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 January 2006 - 05:21 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 frustrated

frustrated
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 15 January 2006 - 05:54 PM

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 :(

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 January 2006 - 07:47 PM

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?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 January 2006 - 10:09 PM

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;

[/quote]
Just put this query in the connection.php of your script.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 frustrated

frustrated
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 15 January 2006 - 10:53 PM

Cool, I'll do that then.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users