以HH:MM格式获取时间差php [英] get difference in time in HH:MM format php

查看:53
本文介绍了以HH:MM格式获取时间差php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取此输出



HH:MM格式?

  $ to_time = strtotime( 2008-12-13 10:42:00); <-AM 
$ from_time = strtotime( 2008-12-14 8:21:00); <-PM
$ stat = round(abs($ to_time-$ from_time)/ 60,2)。 min;

我从中得到的是1299分钟



但我不知道如何输出



21h:41m

解决方案

首先,除非您另行指定 DateTime :: createFromFormat() 8:21:00 将被解释为8AM。 c $ c>。



要计算出时差,请使用 DateTime :: diff()

  $ to = new DateTime( 2008-12-13 10:42:00); 
$ from = new DateTime( 2008-12-14 8:21:00);

$ stat = $ to-> diff($ from); // DateInterval对象

echo $ stat-> format(’%Hh:%Im’);

这将显示两次之间的小时/分钟差,但最多显示24小时。 / p>

如果需要超过24小时,则应执行以下操作:

  $ hours = $ stat->天* 24 + $ stat-&h; 
$ minutes = $ stat-> i;

printf(’%02sh:%sm’,$ hours,$ minutes);


how can i get this to output

HH:MM format?

 $to_time = strtotime("2008-12-13 10:42:00");  <--AM
 $from_time = strtotime("2008-12-14 8:21:00");  <-- PM
 $stat = round(abs($to_time - $from_time) / 60,2). "min";

what i got from this is 1299 mins

but i cant figure out how to make it output

21h:41m

解决方案

Firstly, 8:21:00 will be interpreted as 8AM unless you specified otherwise using DateTime::createFromFormat().

To work out time differences, use DateTime::diff():

$to = new DateTime("2008-12-13 10:42:00");
$from = new DateTime("2008-12-14 8:21:00");

$stat = $to->diff($from); // DateInterval object

echo $stat->format('%Hh:%Im');

This will display the hour/minute difference between the two times, but only up to 24 hours.

If you need more than 24 hours, you should do the following:

$hours   = $stat->days * 24 + $stat->h;
$minutes = $stat->i;

printf('%02sh:%sm', $hours, $minutes);

这篇关于以HH:MM格式获取时间差php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆