使用DateTime :: diff()返回两个日期之间的天数 [英] Using DateTime::diff() to return days between two dates

查看:626
本文介绍了使用DateTime :: diff()返回两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用DateTime :: diff()返回两个日期之间的天,小时,分钟和秒.

I am currently using DateTime::diff() to return the days, hours, minutes, and seconds between two dates.

问题是,当差异超过30天而不是返回33天时,它将返回1个月零3天.

The problem is when the differences is more than 30 days, instead of returning 33 days it returns 1 month and 3 days.

有没有办法告诉它返回几天而不是一个月.

Is there a way to tell it to return days and not month accordingly.

一些代码:

$date1      =   new DateTime(date('Y-m-d H:i:s', time()));
$date2      =   new DateTime($row['start']);

$interval   =   $date1->diff($date2);

$days       =   $newInfo[$key]['days'] = $interval->format("%d"); 
$hours      =   $newInfo[$key]['hours'] = $interval->format("%h"); 
$minutes    =   $newInfo[$key]['minutes'] = $interval->format("%i"); 
$seconds    =   $newInfo[$key]['seconds'] = $interval->format("%s"); 

推荐答案

对于这种确切的情况,有一个特殊的格式参数:

There's a special format parameter for this exact case:

<?php
$days = $newInfo[$key]['days'] = $interval->format("%a");

这篇关于使用DateTime :: diff()返回两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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