Php 我可以得到特定时区的时间戳吗?
我有一个MySQL查询,它将注释保存在数据库中,并保存时间戳。我想将时间戳保存在另一个时区设置中。我可以通过以下查询执行此操作:Php 我可以得到特定时区的时间戳吗?,php,mysql,sql,timestamp,Php,Mysql,Sql,Timestamp,我有一个MySQL查询,它将注释保存在数据库中,并保存时间戳。我想将时间戳保存在另一个时区设置中。我可以通过以下查询执行此操作: INSERT INTO tahminler (comment, comment_text, match_id, user_id, timestamp) VALUES ('$comment','$comment_text', $id, $user_id, now()) 对。您可以在PHP上使用date\u default\u timezone\u set()来完成
INSERT INTO tahminler
(comment, comment_text, match_id, user_id, timestamp)
VALUES ('$comment','$comment_text', $id, $user_id, now())
对。您可以在PHP上使用
date\u default\u timezone\u set()
来完成此操作
<?php
echo date('Y-m-d H:i:s');// Your local server time
date_default_timezone_set('America/Los_Angeles');
echo "<br>";
echo date('Y-m-d H:i:s');// Los Angeles time
你可以试试这个
//default timezone
$date = new DateTime(null);
echo 'Default timezone: '.$date->getTimestamp().'<br />'."\r\n";
//America/New_York
$date = new DateTime(null, new DateTimeZone('America/New_York'));
echo 'America/New_York: '.$date->getTimestamp().'<br />'."\r\n";
//默认时区
$date=新日期时间(空);
回显“默认时区:”。$date->getTimestamp()。
。“\r\n”;
//美国/纽约
$date=新日期时间(空,新日期时区(“美国/纽约”);
回显“美国/纽约:”.$date->getTimestamp()。
。“\r\n”;
获取常规时间戳并添加UTC偏移量
//America/New_York
$date = new DateTime(null, new DateTimeZone('America/New_York'));
echo 'America/New_York: '.($date->getTimestamp() + $date->getOffset()).'<br />'."\r\n";
//美国/纽约
$date=新日期时间(空,新日期时区(“美国/纽约”);
回显“美国/纽约:”。($date->getTimestamp()+$date->getOffset())。“
”。“\r\n”;
您可以使用添加列时间戳来更改表,如下所示
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
这将添加一个名为“LastUpdate”的列,默认值为当前日期/时间。当记录被更新时(比如5分钟后),时间戳将自动更新到当前时间。MySQL时间戳和日期时间列没有时区的概念。您必须添加一个附加列来存储自己的时区数据。只有在a)使用系统的每个人都在同一时区或b)每个用户都有一些时区记录时,这才有效。是的,完全正确。对于选项b),如果已经有一条记录,OP可以从该选项运行switch语句。