php php时间戳之间的时差(以小时为单位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9732553/
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
Time difference between php timestamps in hours
提问by blasteralfred Ψ
I have a php timestamp 1331875634
generated using php time()
function.
我有一个1331875634
使用 phptime()
函数生成的 php 时间戳。
I have the current timestamp generated using same function.
我有使用相同函数生成的当前时间戳。
<?php
$time1 = "1331875634";
$time2 = time();
echo $differencem; //time difference in minutes
echo $differenceh; //time difference in hours
?>
I want to know the difference between these two in minutes. The minutes may be divided by 60 to make it in hours.
我想在几分钟内知道这两者之间的区别。分钟可以除以 60 以使其以小时为单位。
回答by DanRedux
You get the different in seconds if you subtract them, so divide it by 60 to get minutes and by 60 again to get hours.
如果你减去它们,你会得到不同的秒数,所以将它除以 60 得到分钟,再除以 60 得到小时。
回答by PHPCoderSkinner
I created this code to take standard PHP UNIX TIMESTAMP, calculate the difference in time and return a standard time or a specialized time format. This is great for timing a project and calculating the time it takes to get the results.
我创建此代码以采用标准 PHP UNIX TIMESTAMP,计算时间差并返回标准时间或专门的时间格式。这对于为项目计时并计算获得结果所需的时间非常有用。
function timerFormat($start_time, $end_time, $std_format = false)
{
$total_time = $end_time - $start_time;
$days = floor($total_time /86400);
$hours = floor($total_time /3600);
$minutes = intval(($total_time/60) % 60);
$seconds = intval($total_time % 60);
$results = "";
if($std_format == false)
{
if($days > 0) $results .= $days . (($days > 1)?" days ":" day ");
if($hours > 0) $results .= $hours . (($hours > 1)?" hours ":" hour ");
if($minutes > 0) $results .= $minutes . (($minutes > 1)?" minutes ":" minute ");
if($seconds > 0) $results .= $seconds . (($seconds > 1)?" seconds ":" second ");
}
else
{
if($days > 0) $results = $days . (($days > 1)?" days ":" day ");
$results = sprintf("%s%02d:%02d:%02d",$results,$hours,$minutes,$seconds);
}
return $results;
}
Example:
$begin_routine_time = time();
echo(timerFormat($begin_routine_time, $time()));
回答by Sadee
$datetime1 = new DateTime(date('Y-m-d H:i:s', 1331875634));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$oDiff = $datetime1->diff($datetime2);
echo $oDiff->y.' Years <br/>';
echo $oDiff->m.' Months <br/>';
echo $oDiff->d.' Days <br/>';
echo $oDiff->h.' Hours <br/>';
echo $oDiff->i.' Minutes <br/>';
echo $oDiff->s.' Seconds <br/>';
回答by CoreCoder
Once I needed to convert seconds to time like 1 day 03:34:13 days hours:minuts:secondes
有一次我需要将秒转换为时间,例如 1 天 03:34:13 days hours:minuts:secondes
I wrote this function
我写了这个函数
function sECONDS_TO_HMS($seconds)
{
$days = floor($seconds/86400);
$hrs = floor($seconds/3600);
$mins = intval(($seconds / 60) % 60);
$sec = intval($seconds % 60);
if($days>0){
//echo $days;exit;
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
$hours=$hrs-($days*24);
$return_days = $days." Days ";
$hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
}else{
$return_days="";
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
}
$mins = str_pad($mins,2,'0',STR_PAD_LEFT);
$sec = str_pad($sec,2,'0',STR_PAD_LEFT);
return $return_days.$hrs.":".$mins.":".$sec;
}
echo sECONDS_TO_HMS(65); // 00:01:05
echo sECONDS_TO_HMS(76325); //21:12:05
echo sECONDS_TO_HMS(345872); // 4 Days 00:04:32
I think it could be helpful for you.
我想它可能对你有帮助。