在php中将字符串转换为时间

在php中将字符串转换为时间,php,date,Php,Date,如何在php中将“2011-11-03T17:27:56Z”转换为时间 我想要和当前时间的时差 i、 e.如果与当前时间的时差为10分钟,则我需要10分钟。如果是1天,那么我想要1天。这个小片段将以秒为单位显示现在和给定日期之间的差异 $dateString = "2011-11-03T17:27:56Z"; $date = strtotime($dateString); $diff = time() - $date; echo $diff; 要提供您要求的特定格式,您可以使用我找到的以下函数

如何在php中将
“2011-11-03T17:27:56Z”
转换为时间

我想要和当前时间的时差


i、 e.如果与当前时间的时差为10分钟,则我需要10分钟。如果是1天,那么我想要1天。

这个小片段将以秒为单位显示现在和给定日期之间的差异

$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo $diff;
要提供您要求的特定格式,您可以使用我找到的以下函数:

产出:

8天


我相信您需要
strotime()
函数:

$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";
}

函数期望得到一个包含英文日期的字符串 格式化,并将尝试将该格式解析为Unix时间戳

工作示例:()


$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo time_diff($diff);
$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";
}
$diffInSecs = time() - strtotime('2011-11-03T17:27:56Z');
<?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);