MySQL now() change timezone

后端 未结 4 1134
庸人自扰
庸人自扰 2020-12-20 13:51

I\'m using the following INSERT statement:

INSERT INTO messages SET `to` = \'\".$to.\"\', `from` = \'\".$this->userid.\"\', `title` = \'\".$title.\"\', `m         


        
相关标签:
4条回答
  • 2020-12-20 14:22

    After you open the connection to MySQL, run the following as a query:

    SET time_zone = timezone;
    

    Then all functions you do will run for that timezone for that connection (i.e. until you close the "link" to the database".

    If you have the appropriate permissions you can lock it "permanently" / globaly. Tiemzone strings are standard strings as you have in your question.

    http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

    0 讨论(0)
  • 2020-12-20 14:23

    You would want to go ahead and use the CONVERT_TZ() function in MySQL. It's based off the Olson database which your operating system uses.

    Here is the documentation.

    0 讨论(0)
  • 2020-12-20 14:31

    Better use the SQL format directly in your query:

    ..`created` = CONVERT_TZ(NOW(),'SYSTEM','Asia/Calcutta')..
    
    0 讨论(0)
  • 2020-12-20 14:35
    $myDateTime = new DateTime('2012-05-23 17:01', new DateTimeZone('GMT'));
    $myDateTime->setTimezone(new DateTimeZone('Asia/Kolkata'));
    echo $myDateTime->format('Y-m-d H:i');
    

    After modification to above code, such as desired format; you could use $myDateTime variable to insert into database.

    0 讨论(0)
提交回复
热议问题