如何检查日期是否不一样(How to check if the dates are not the same)

我尝试编写一个脚本,可以计算两个日期之间的差异,以天为单位, diff不符合我的需要。

更具体地说,我们有以下两个日期/时间:

2015-03-18 23:00

2015-03-19 02:00

实际时差是四个小时,在这个术语中diff工作正常!

但我想知道的是,如果日历日期已经改变,那么实际的差异是什么。

因此,在上面的示例中,日历日期有1天的差异。

在以下示例中

2015-03-18 23:00

2015-03-21 02:00

我有三天的差异。 那么我该如何计算这个日期差异呢?

目前我使用以下代码:

$datetime1 = new DateTime('2009-10-11 23:30'); $datetime2 = new DateTime('2009-10-12 02:30'); $interval = $datetime1->diff($datetime2); echo "<pre>"; print_r($interval); echo "</pre>";

结果如下:

DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 3 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

有什么想法吗? 谢谢 ...

I try to write a script that can calculate the difference between two dates, in days, and the diff does not behave as I need to.

More specific, lets say we have the following two date/times:

2015-03-18 23:00

2015-03-19 02:00

The actual time difference is four hours, and in this terms the diff works fine !

But what I like to know is, if the calendar date has been change, and what is the actual difference.

So in the example above, the calendar dates having 1 day difference.

In the following example

2015-03-18 23:00

2015-03-21 02:00

I have three days difference. So how can I calculate this date difference ?

At the moment I use the following code:

$datetime1 = new DateTime('2009-10-11 23:30'); $datetime2 = new DateTime('2009-10-12 02:30'); $interval = $datetime1->diff($datetime2); echo "<pre>"; print_r($interval); echo "</pre>";

and the result is the following:

DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 3 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

Is there any idea ? Thanks ...

最满意答案

非常简单的解决方案

注意 :这仅在天数在同一个月时有效

$datetime1 = new DateTime('2015-03-18 23:00'); $datetime2 = new DateTime('2015-03-21 02:00'); $difference = $datetime2->format('d') - $datetime1->format('d'); //3

清洁解决方案

你可以删除日期中的所有内容,但是年,月和日,并使用diff(),就像你已经做的那样。

$datetime1 = new DateTime('2015-03-18 23:00'); $datetime2 = new DateTime('2015-03-21 02:00'); $datetime1modified = new DateTime($datetime1->format('Y-m-d')); $datetime2modified = new DateTime($datetime2->format('Y-m-d')); $difference = $datetime1modified->diff($datetime2modified)->d; //3

A really simple solution

Notice: this will only work if the days are in the same month.

$datetime1 = new DateTime('2015-03-18 23:00'); $datetime2 = new DateTime('2015-03-21 02:00'); $difference = $datetime2->format('d') - $datetime1->format('d'); //3

Clean solution

You could remove everything from the date, but the year, month and day and use diff() as you already did.

$datetime1 = new DateTime('2015-03-18 23:00'); $datetime2 = new DateTime('2015-03-21 02:00'); $datetime1modified = new DateTime($datetime1->format('Y-m-d')); $datetime2modified = new DateTime($datetime2->format('Y-m-d')); $difference = $datetime1modified->diff($datetime2modified)->d; //3

更多推荐