Php 在不转换到unix的情况下向时间戳添加小时数?
我需要在从数据库获取的时间戳上加上+7小时,但我不能将其更改为unix,因为我正在使用一个函数将其转换为“2小时前”之类的内容 我该怎么做呢?我看到的每个问题都使用Php 在不转换到unix的情况下向时间戳添加小时数?,php,timestamp,strtotime,Php,Timestamp,Strtotime,我需要在从数据库获取的时间戳上加上+7小时,但我不能将其更改为unix,因为我正在使用一个函数将其转换为“2小时前”之类的内容 我该怎么做呢?我看到的每个问题都使用strotime(),这与我使用的函数不兼容 下面是函数: function time_passed($datetime, $full = false) { $datetimes = $datetime; $now = new DateTime; $ago = new DateTime($datetimes);
strotime()
,这与我使用的函数不兼容
下面是函数:
function time_passed($datetime, $full = false) {
$datetimes = $datetime;
$now = new DateTime;
$ago = new DateTime($datetimes);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
例如,我用
2014-09-11 05:14:12
来处理这个函数,它在12小时前输出12小时前
——尽管我希望它在19小时前输出19小时前
为什么要添加+7小时?看起来您正在尝试替换时区差异。为什么不直接声明带有时区设置的$now
变量,让php为您创造奇迹呢<代码>新日期时间(“现在”,新日期时区(“美国/洛杉矶”)代码>或任何你的时区。您可以在上找到时区列表,向我们展示示例数据和您所了解的内容tried@JohnConde是的,一秒钟,你就不能把它转换成一个unix时间戳,然后再转换回你的函数所需要的任何格式吗?你得到的“时间戳”是什么格式?@JonathanKuhn看到更新的帖子了。你为什么要加上+7小时?看起来您正在尝试替换时区差异。为什么不直接声明带有时区设置的$now
变量,让php为您创造奇迹呢<代码>新日期时间(“现在”,新日期时区(“美国/洛杉矶”)代码>或任何你的时区。您可以在以下位置找到时区列表: