简体   繁体   English

本地时区的PHP日期函数,UTC的NOW现在

[英]PHP date function in local timezone and NOW in UTC

In my code, $now = date("Ymd H:i:s") returns the time in UTC. 在我的代码中, $now = date("Ymd H:i:s")返回UTC时间。 But when using UPDATE to inject let say 'last_login' = NOW() , it returns the time according to my timezone. 但是,当使用UPDATE进行注入时,说'last_login' = NOW() ,它将根据我的时区返回时间。 This posts problem when you want to compare between the two time. 当您想在两个时间之间进行比较时,这会引发问题。 I want to keep things in UTC, so is there a way to UPDATE the 'last_login' field in UNIX or UTC time as well? 我想将事情保留在UTC中,所以有没有办法在UNIX或UTC时间中更新'last_login'字段呢?

$now=gmdate("Y-m-d H:i:s");

gmdate()与日期相同,但带有格林尼治标准时间

Try this 尝试这个

<?php 
echo date('Y-m-d H:i:s T', time()) . "\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time());
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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