如何计算PHP中的时差? [英] How to calculate time difference in PHP?

查看:77
本文介绍了如何计算PHP中的时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须计算日期时间差,如何在PHP中做到这一点?我需要确切的小时,分​​钟和秒.有人有脚本吗?

I have to calculate date time difference, how to do that in PHP? I need exact hours, mins and secs. Anybody have the scripts for that?

推荐答案

使用 PHP的 DateTime类的diff()方法:-

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 对象:-

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屋!

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