Mysql And PHP time varies ?

Discussion in 'PHP' started by xkaser101, Aug 30, 2011.

  1. #1
    Hello,

    I am having this wierd issue, probably have to due with time zones...

    Anyway when I do

    SELECT NOW();
    Code (markup):
    query in Mysql it returns "2011-08-30 22:46:28" but when I do

     echo date("Y-m-d  h:m:s",time());
    Code (markup):
    In PHP it returns "2011-08-30 07:08:09"

    Any Idea how to make them return the same results or better ways to store time ETC.... ? :confused:

    Thanks in advance,
    Kind Regards.
    Issa Shamoun.
     
    Solved! View solution.
    xkaser101, Aug 30, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    under <?php place
    date_default_timezone_set('UTC'); // UTC, GMT or whatever zone your looking to set 
    PHP:
     
    Last edited: Aug 30, 2011
    MyVodaFone, Aug 30, 2011 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Did you run these at the same time? You shouldn't have servers 30 minutes apart no matter what method you are using to standardize the times.
     
    jestep, Aug 30, 2011 IP
  4. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yep same exact time.
     
    xkaser101, Aug 30, 2011 IP
  5. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I will check this one out and see for my self but is there anyway to change mysql time zone instead of php ?
     
    xkaser101, Aug 30, 2011 IP
  6. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #6
    MySQL returns your server's current time no matter what timezone your in, while PHP returns UTC time (GMT)
     
    JohnnySchultz, Aug 31, 2011 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    MySql stores values from your input, ie: your php INSERT, so you should perhaps alter your INSERT code to reflect the time of input from your time zone....
    date_default_timezone_set('UTC'); // UTC, GMT or whatever zone your looking to set
    Code (markup):
    ...rather then letting the mysql server determine it.
     
    MyVodaFone, Aug 31, 2011 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    If this is on your own computer, you have a major problem (the 30 minute difference). If this is on a site you buy hosting for, call tech support at your host and ask them to time-synchronize their servers. They have MySQL running on a different computer than the site is running on (which is common) and the two computers have different times.
     
    Rukbat, Aug 31, 2011 IP
  9. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hello i am running this test on my own PC using WAMP. I have a paid hosting i will try it on.
     
    xkaser101, Aug 31, 2011 IP
  10. #10
    Rukbat, Aug 31, 2011 IP
  11. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    hahaahaha ;) of all the above comments no one figured that out :) Thanks +Rep !
     
    xkaser101, Aug 31, 2011 IP
  12. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #12
    *I* didn't figure it out, the PHP interpreter did. I stuck the code into my test file and noticed that the "minute" was the month, even as the time advanced. The date() function wants the format in "date" in the chm file, not the one in "date formats". You also need date_default_timezone_set() set to the timezone (UTC or your own) to get the correct hour. There's a list of the valid ones in the manual (online or the enhanced chm file - the one with the comments).
     
    Rukbat, Sep 1, 2011 IP
  13. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks anyway :)
     
    xkaser101, Sep 1, 2011 IP