Php 相对日期格式设置始终输出“;“3小时前”;
我正在使用php计算指定日期和当前日期之间的日期间隔。我这样做是为了打印出社交友好的时间戳,比如几分钟前和两小时前 当我谈到小时部分时,我会发现php中的一些行为非常有趣。下面是完整的工作代码,但当您用此代码替换Php 相对日期格式设置始终输出“;“3小时前”;,php,date-formatting,Php,Date Formatting,我正在使用php计算指定日期和当前日期之间的日期间隔。我这样做是为了打印出社交友好的时间戳,比如几分钟前和两小时前 当我谈到小时部分时,我会发现php中的一些行为非常有趣。下面是完整的工作代码,但当您用此代码替换hours零件时,它总是打印3小时 固定日期 // The current date timestamp define('DATE', time()); 错误代码如下: 您的代码运行良好。 您调用它的方式有错误。 当$date作为unix时间戳传递时,它运行得非常好 例如$date=t
hours
零件时,它总是打印3小时
固定日期
// The current date timestamp
define('DATE', time());
错误代码如下:
您的代码运行良好。
您调用它的方式有错误。
当$date作为unix时间戳传递时,它运行得非常好
例如$date=time()-1*60*60(用于1小时前)
date('H',3660)
对我来说是01。可能是应用您所在的时区。可以是本地级别(应用程序)或全局级别(php配置)。甚至可能是你的操作系统。很抱歉,我无法提供更多帮助来查找哪些PHP设置等等。日期()的结果取决于您的时区。您可以通过按日期手动设置时区来更改此行为。\u default\u timezone\u set()
function do_plurals($nb, $str)
{
return $nb > 1 ? $str . 's' : $str;
}
function dates_interval_text(DateTime $start, DateTime $end)
{
$interval = $end->diff($start);
$format = array();
if ($interval->y !== 0)
{
$format[] = "%y " . do_plurals($interval->y, "year");
}
if ($interval->m !== 0)
{
$format[] = "%m " . do_plurals($interval->m, "month");
}
if ($interval->d !== 0)
{
$format[] = "%d " . do_plurals($interval->d, "day");
}
if ($interval->h !== 0)
{
$format[] = "%h " . do_plurals($interval->h, "hour");
}
if ($interval->i !== 0)
{
$format[] = "%i " . do_plurals($interval->i, "minute");
}
if (!count($format))
{
return "less than a minute ago";
}
// We use the two biggest parts
if (count($format) > 1)
{
$format = array_shift($format) . " and " . array_shift($format);
}
else
{
$format = array_pop($format);
}
// Prepend 'since ' or whatever you like
return $interval->format($format) . ' ago';
}
它产生的结果类似于2小时3分钟前的
日期的第二个参数不是持续时间,而是时间戳<代码>日期
回答问题“1970年1月1日00:00 UTC后N秒,现在是什么时间?”并且此问题的答案在您的时区中给出,因此您的时区偏移量将添加到答案中
使用
date('Z')
查找时区偏移量,以秒为单位。或者,更好的方法是使用下限($time/3600)
计算小时数。我的第一个问题是,你从哪里获得时间/时间戳/日期时间
在我们公司,我们在德国运行一些服务器,服务器上托管的应用程序是为巴基斯坦(GMT+5)开发的。我们使用此函数获取Unix时间戳以返回“人类可读”的日期时间:-
/**
* @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
* @param number $timestamp
* @return string
*/
function readable_time($timestamp, $num_times = 2) {
// this returns human readable time when it was uploaded
$times = array (
31536000 => 'year', 2592000 => 'month',
604800 => 'week', 86400 => 'day',
3600 => 'hour', 60 => 'minute', 1 => 'second'
);
$now = time ();
$secs = $now - $timestamp;
// Fix so that something is always displayed
if ($secs == 0) $secs = 1;
$count = 0; $time = '';
foreach ( $times as $key => $value ) {
if ($secs >= $key) {
// time found
$s = '';
$time .= floor ( $secs / $key );
if ((floor ( $secs / $key ) != 1)) $s = 's';
$time .= ' ' . $value . $s;
$count ++;
$secs = $secs % $key;
if ($count > $num_times - 1 || $secs == 0) break;
else $time .= ', ';
}
}
return ($time != "") ? $time." ago" : "now";
}
通过修改$num_times
变量,我们可以得到类似“4个月、3天、2小时、3分钟、15秒前”(在本例中,将变量设置为5)的结果
从逻辑上讲,时间戳来自一个数据库(在我们的例子中是MySQL),因此我们使用另一个函数来获取MySQL时间戳并将其转换为Unix时间戳:-
/**
* @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
* @param number $timestamp
* @return string
*/
function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
祝你好运 我真的想到了并且检查了一下,我的时区都很好。Thx虽然“您的时区都很好”意味着应用的时区是您当前的时区?或者说PHP正在使用UTC?它确实应该使用UTC进行计算,如Victor所说,请检查
date('Z')
。您应该注意,您的代码使用了。是的,我知道。原生DateTime类包含在PHP5.2.0中,所以我认为这不是一个问题。是的,但我第一眼就有点不确定,只是看到了一堆类引用。只有dates\u interval\u text()?我猜你已经准备好用数学来计算你的时间间隔了。我真的不知道,我想我喜欢这样一个事实,date()
在格式等方面为我做了工作。它也更容易维护。时区绝对有意义=D,你看这是你凌晨3点工作时发生的事情,我的时区是GMT+2,因此我的输出是03而不是01。太好了,谢谢你。
function do_plurals($nb, $str)
{
return $nb > 1 ? $str . 's' : $str;
}
function dates_interval_text(DateTime $start, DateTime $end)
{
$interval = $end->diff($start);
$format = array();
if ($interval->y !== 0)
{
$format[] = "%y " . do_plurals($interval->y, "year");
}
if ($interval->m !== 0)
{
$format[] = "%m " . do_plurals($interval->m, "month");
}
if ($interval->d !== 0)
{
$format[] = "%d " . do_plurals($interval->d, "day");
}
if ($interval->h !== 0)
{
$format[] = "%h " . do_plurals($interval->h, "hour");
}
if ($interval->i !== 0)
{
$format[] = "%i " . do_plurals($interval->i, "minute");
}
if (!count($format))
{
return "less than a minute ago";
}
// We use the two biggest parts
if (count($format) > 1)
{
$format = array_shift($format) . " and " . array_shift($format);
}
else
{
$format = array_pop($format);
}
// Prepend 'since ' or whatever you like
return $interval->format($format) . ' ago';
}
/**
* @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
* @param number $timestamp
* @return string
*/
function readable_time($timestamp, $num_times = 2) {
// this returns human readable time when it was uploaded
$times = array (
31536000 => 'year', 2592000 => 'month',
604800 => 'week', 86400 => 'day',
3600 => 'hour', 60 => 'minute', 1 => 'second'
);
$now = time ();
$secs = $now - $timestamp;
// Fix so that something is always displayed
if ($secs == 0) $secs = 1;
$count = 0; $time = '';
foreach ( $times as $key => $value ) {
if ($secs >= $key) {
// time found
$s = '';
$time .= floor ( $secs / $key );
if ((floor ( $secs / $key ) != 1)) $s = 's';
$time .= ' ' . $value . $s;
$count ++;
$secs = $secs % $key;
if ($count > $num_times - 1 || $secs == 0) break;
else $time .= ', ';
}
}
return ($time != "") ? $time." ago" : "now";
}
/**
* @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
* @param number $timestamp
* @return string
*/
function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}