在PHP中将DateInterval对象转换为秒 [英] Convert DateInterval object to seconds in php

查看:238
本文介绍了在PHP中将DateInterval对象转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);

我如何将以上$interval转换为php中的秒数

How do i convert the above $interval to seconds in php

推荐答案

为此提供了一个功能format.但是它不会返回秒数.要获得秒数,请使用此技术

There is a function format for this. But it wont return the number of seconds. To get number of seconds you use this technique

$seconds = abs($datetime1->getTimestamp()-$datetime2->getTimestamp());

如果您确实要使用$interval,则需要进行计算.

If you really want to use $interval you need to calculate it.

$seconds = $interval->days*86400 + $interval->h*3600 
           + $interval->i*60 + $interval->s;

这里

  • 86400是一天中的秒数
  • 3600是小时中的秒数
  • 60是每分钟的秒数
  • 86400 is the number of seconds in a day
  • 3600 is the number of seconds in an hour
  • 60 is the number of seconds in a minute

这篇关于在PHP中将DateInterval对象转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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