转换时间戳的时区差异
我找到了这个类,用于将时间戳转换为 X 时间前。 除了一个问题之外,它运行得很好。我位于太平洋时区,因此时间总是显示 8 小时前,而实际上应该显示 2 秒前。
class Cokidoo_DateTime extends DateTime {
protected $strings = array(
'y' => array('1 year ago', '%d years ago'),
'm' => array('1 month ago', '%d months ago'),
'd' => array('1 day ago', '%d days ago'),
'h' => array('1 hour ago', '%d hours ago'),
'i' => array('1 minute ago', '%d minutes ago'),
's' => array('now', '%d secons ago'),
);
/**
* Returns the difference from the current time in the format X time ago
* @return string
*/
public function __toString() {
$now = new DateTime('now');
$diff = $this->diff($now);
foreach($this->strings as $key => $value){
if( ($text = $this->getDiffText($key, $diff)) ){
return $text;
}
}
return '';
}
/**
* Try to construct the time diff text with the specified interval key
* @param string $intervalKey A value of: [y,m,d,h,i,s]
* @param DateInterval $diff
* @return string|null
*/
protected function getDiffText($intervalKey, $diff){
$pluralKey = 1;
$value = $diff->$intervalKey;
if($value > 0){
if($value < 2){
$pluralKey = 0;
}
return sprintf($this->strings[$intervalKey][$pluralKey], $value);
}
return null;
}
}
如何通过 SQL 插入新行:
mysql_query("INSERT INTO `" . $dbMain . "`.`" . $dbTable . "` (`title`, `date`) VALUES ('$fileTitle', NOW())");
默认设置为 CURRENT_TIMESTAMP;
我该如何修改我的方法才能使其发挥作用? 理想情况下,我希望这对每个人来说都是通用的,因此无论您位于哪个时区,它都会根据新条目的存在时间显示 X 时间之前。
I've got this class I found for converting timestamps to X Time Ago.
It works great, except for one problem. I'm in the Pacific Time Zone, and so the time always says 8 hours ago, when it should really say 2 seconds ago.
class Cokidoo_DateTime extends DateTime {
protected $strings = array(
'y' => array('1 year ago', '%d years ago'),
'm' => array('1 month ago', '%d months ago'),
'd' => array('1 day ago', '%d days ago'),
'h' => array('1 hour ago', '%d hours ago'),
'i' => array('1 minute ago', '%d minutes ago'),
's' => array('now', '%d secons ago'),
);
/**
* Returns the difference from the current time in the format X time ago
* @return string
*/
public function __toString() {
$now = new DateTime('now');
$diff = $this->diff($now);
foreach($this->strings as $key => $value){
if( ($text = $this->getDiffText($key, $diff)) ){
return $text;
}
}
return '';
}
/**
* Try to construct the time diff text with the specified interval key
* @param string $intervalKey A value of: [y,m,d,h,i,s]
* @param DateInterval $diff
* @return string|null
*/
protected function getDiffText($intervalKey, $diff){
$pluralKey = 1;
$value = $diff->$intervalKey;
if($value > 0){
if($value < 2){
$pluralKey = 0;
}
return sprintf($this->strings[$intervalKey][$pluralKey], $value);
}
return null;
}
}
How I insert new rows via SQL:
mysql_query("INSERT INTO `" . $dbMain . "`.`" . $dbTable . "` (`title`, `date`) VALUES ('$fileTitle', NOW())");
Default is set to CURRENT_TIMESTAMP;
How can I modify my method to get this to work?
Ideally, I'd like this to be universal for everyone, so no matter what time zone you're in, it will shows X time ago based on when a new entry is in existence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这请求当地时间。但你想要 UTC 时间。 指定 UTC 作为构造函数的第二个参数。
This requests local time. But you want UTC time. Specify UTC as a second parameter to the constructor.