将字符串转换为php中的时间 [英] Convert string to time in php
本文介绍了将字符串转换为php中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何转换2011-11-03T17:27:56Z
到PHP时间。
How to convert "2011-11-03T17:27:56Z"
to time in php.
我想要与当前时间的时差。
I want time difference from current time.
ie如果与当前时间的差距是10分钟,我想要10分钟。如果它的1天,那么我想要1天。
i.e. if time difference from current time is 10 minutes the I want 10 minutes. If its 1 day then I want 1 day.
推荐答案
这个小代码片段将给你现在和给定之间的秒数日期。
This little snippet will give you the difference in seconds between now and the given date.
$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo $diff;
要给出您要求的具体格式,您可以使用以下功能,我发现这里:
To give it the specific format you're asking for you can use the below function I found here:
function time_diff($s) {
$m = 0; $hr = 0; $d = 0; $td = "now";
if ($s > 59) {
$m = (int)($s/60);
$s = $s-($m*60); // sec left over
$td = "$m min";
}
if ($m > 59) {
$hr = (int)($m / 60);
$m = $m - ($hr*60); // min left over
$td = "$hr hr";
if ($hr > 1) {
$td .= "s";
}
if ($m > 0) {
$td .= ", $m min";
}
}
if ($hr > 23) {
$d = (int) ($hr / 24);
$hr = $hr-($d*24); // hr left over
$td = "$d day";
if ($d > 1) {
$td .= "s";
}
if ($d < 3) {
if ($hr > 0) {
$td .= ", $hr hr";
}
if ($hr > 1) {
$td .= "s";
}
}
}
return $td;
}
结合这两者,您将获得以下信息:
Combining the both this is what you get:
$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo time_diff($diff);
输出:
8天
8 days
这篇关于将字符串转换为php中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文