php 在php中将字符串转换为时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8097922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to time in php
提问by Muhammad Imran Tariq
How to convert "2011-11-03T17:27:56Z"
to time in php.
如何"2011-11-03T17:27:56Z"
在php中转换为时间。
I want time difference from current time.
我想要与当前时间的时差。
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.
即如果与当前时间的时差是 10 分钟,我想要 10 分钟。如果是 1 天,那么我想要 1 天。
回答by Marcus
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);
Outputs:
输出:
8 days
8天
回答by Moe Sweet
$diffInSecs = time() - strtotime('2011-11-03T17:27:56Z');
回答by Benjamin Crouzier
Working example: (codepad here)
工作示例:(此处为键盘)
<?php
$time_str = "2011-11-03T17:27:56Z";
//echo date('d/m/y H:i:s', strtotime($time_str));
$diff = abs(strtotime("now") - strtotime($time_str));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
(time diff took here: How to calculate the difference between two dates using PHP?)
(这里的时间差异:如何使用 PHP 计算两个日期之间的差异?)
回答by Jasper
I believe you want the strtotime()
function:
我相信你想要这个strtotime()
功能:
$some_time = strtotime("2011-11-03T17:27:56Z");//outputs a UNIX TIMESTAMP
$time_diff = (time() - $some_time);
if ($time_diff > 86400) {
echo round($time_diff / 86400) . " days";
} else if ($time_diff > 3600) {
echo round($time_diff / 3600) . " hours";
} else {
echo round($time_diff / 60) . " minutes";
}
http://us.php.net/manual/en/function.strtotime.php
http://us.php.net/manual/en/function.strtotime.php
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp
该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳