日期差异仅考虑年月日 [英] Date difference only considering year month day

查看:151
本文介绍了日期差异仅考虑年月日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算日期差异,只考虑年,月,日在php。现在程序计算日期差异考虑到所有的日期时间对象参数。

I want to calculate date difference only considering year,month and day in php. Now the program calculate date difference considering all of the date time object parameter.

$DTObj = new DateTime();
$pdate = DateTime::createFromFormat("ymd", 141118);
$diff = $DTObj->diff($pdate);
$dday = $diff->format("%a");

我如何实现这一点?

推荐答案

阅读并尝试:

# create DateTime object with time at 00:00:00
$from = new DateTime('today'); 

# create DateTime object from ymd format
# format character ! resets all fields (year, month, ...) to the Unix Epoch and then sets the inputed date
$to = DateTime::createFromFormat('!ymd', '141118');

# calculate difference between $from and $to and return DateInterval object
$diff = $from->diff($to);

# echo difference of total number of days
echo $diff->format('%a');

演示

这篇关于日期差异仅考虑年月日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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