如何计算PHP中的时差 [英] how to calculate time difference in PHP
本文介绍了如何计算PHP中的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在PHP中计算日期时差。
我需要精确的小时,分和秒。任何身体都有脚本,请给我:-P
I Have to calculate date time difference how to do that in PHP. I need exact hours , mins and secs. any body have the scripts for that please give me :-P
推荐答案
使用 diff()方法 =nofollow noreferrer> DateTime类这样: -
Use the diff() method of PHP's DateTime class like this:-
$lastWeek = new DateTime('last thursday');
$now = new DateTime();
var_dump($now->diff($lastWeek, true));
这将给你一个 DateInterval 对象: -
This will give you a DateInterval object:-
object(DateInterval)[48]
public 'y' => int 0
public 'm' => int 0
public 'd' => int 2
public 'h' => int 11
public 'i' => int 34
public 's' => int 41
public 'invert' => int 0
public 'days' => int 2
从中检索您想要的值是微不足道的。
Retrieving the values you want from that is trivial.
这篇关于如何计算PHP中的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文