PHP仅与时间一起工作 [英] php working with time only

查看:81
本文介绍了PHP仅与时间一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php脚本中使用时间戳来处理时间,以小时,分钟和秒为单位。时间戳记也记录日期,但我仅使用时间 00:00:00

I'm using timestamp in my php script to handle time in hours, minutes and seconds. Timestamps also record the date but I'm only using time 00:00:00.

例如,我m计算时间差

$start = strtotime("12:00:00");
$end = strtotime("20:00:00");

$difference = $end - $start;

或除法时间

$ divide = $ difference / 3;

我应该继续对这些操作使用时间戳还是在其中存在特定于时间的功能php?

Should I continue using timestamps for these operations or is there a time-specific function in php?

推荐答案

如何使用 DateTime 做到这一点的示例:

An example of how to do this with DateTime:

$d1 = new DateTime('11:00:00');
$d2 = new DateTime('04:00:00');

$diff = $d1->diff($d2);

echo $diff->h, " hours ", $diff->i, " minutes\n";

输出:

7 hours 0 minutes

这篇关于PHP仅与时间一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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