简体   繁体   中英

MySQL now() change timezone

I'm using the following INSERT statement:

INSERT INTO messages SET `to` = '".$to."', `from` = '".$this->userid."', `title` = '".$title."', `message` = '".$message."', `created` = NOW()

However, it uses my server time (America/Montreal). I want time zone of Asia (Asia/Calcutta)

Is this possible with the same query?

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.

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

最好在查询中直接使用SQL格式:

..`created` = CONVERT_TZ(NOW(),'SYSTEM','Asia/Calcutta')..
$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM