Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-减去时间不返回任何内容_Php_Time_Strtotime - Fatal编程技术网

PHP-减去时间不返回任何内容

PHP-减去时间不返回任何内容,php,time,strtotime,Php,Time,Strtotime,我找到了一个函数形式的SC输出人类可读的时间。像` 5小时、1小时、5年等 功能人工时间($time) { $time=time()-strotime($time);//获取从那一刻开始的时间 $tokens=数组( 31536000=>“年”, 2592000=>“月”, 604800=>“一周”, 86400=>“天”, 3600=>“小时”, 60=>“分钟”, 1=>“秒” ); foreach($unit=>$text形式的令牌){ 如果($时间

我找到了一个函数形式的SC输出人类可读的时间。像`

5小时、1小时、5年等

功能人工时间($time)
{
$time=time()-strotime($time);//获取从那一刻开始的时间
$tokens=数组(
31536000=>“年”,
2592000=>“月”,
604800=>“一周”,
86400=>“天”,
3600=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($unit=>$text形式的令牌){
如果($时间<$单位)继续;
$numberOfUnits=楼层($time/$unit);
返回$numberOfUnits.'.$text.($numberOfUnits>1)?'s':'';
}
}
我的时间是字符串:
2013-09-28 20:55:42

当我调用此函数时,
human\u time('2013-09-28 20:55:42')

那么它什么也不返回,为什么? 我在上述函数中添加了
strotime


请告诉我出了什么问题。

这不是现成的代码,而是用来指导您正确的方法:

$then = new DateTime($time); // you might need to format $time using strtotime or other functions depending on the format provided
$now = new DateTime();
$diff = $then->diff($now, true);
echo $diff->format('Your style goes here');
有关更多文档,请参阅,或在此处询问

编辑:链接已修复。

使用示例:
echo time_exposed_string('2013-05-01 00:22:35');
回音时间字符串('2013-05-01 00:22:35',正确);
输出:
4个月前
4个月2周3天1小时49分15秒前

您是否考虑过使用php的
DateTime
类来达到您的目的?我想没有必要在这里重新发明轮子。我试着在谷歌上搜索,我在stackoverflow上找到了这个功能,请建议是否已经有了这个功能。这对你有用吗?它至少会向你展示一种方法。@vascowhite,谢谢它的帮助。看一看。
$then = new DateTime($time); // you might need to format $time using strtotime or other functions depending on the format provided
$now = new DateTime();
$diff = $then->diff($now, true);
echo $diff->format('Your style goes here');