如何减去两个日期,忽略PHP中的夏令时?

时间:2023-02-07 16:29:48

I'm trying to calculate the number of days between two days, but I'm running into issues with Daylight Savings Time. Here's my code:

我正在尝试计算两天之间的天数,但我遇到了夏令时的问题。这是我的代码:

function date_diff($old_date, $new_date) {
    $offset = strtotime($new_date) - strtotime($old_date);
    return $offset/60/60/24;
}

Works fine as long as the days are both within the same DST period:

只要日期都在同一个DST期间,就可以正常工作:

echo date_diff('3/15/09', '3/18/09'); // 3

But not if they're further apart:

但如果它们相距甚远则不是:

echo date_diff('11/15/08', '3/18/09'); // 122.95833333333

I want an even number of days, and don't care about DST. I suppose I could round the result, but that feels kludgy. Is there a better (easy) way? I don't want to have to write a whole day-parsing-and-counting-avoiding-leap-years thing if I can avoid it.

我想要偶数天,而不关心夏令时。我想我可以绕过结果,但这感觉很糟糕。有更好的(简单的)方式吗?如果我可以避免它,我不想写一整天解析和计数 - 避免闰年的事情。

(Note: this has to run under php 5.1.6, so some of the date features in 5.3 may not be available.)

(注意:这必须在php 5.1.6下运行,因此5.3中的某些日期功能可能无法使用。)

A bit more info: I'm going to take the offset and add it to other datetimes that are in a db, and I want only the day part to change, not the time part. Turns out rounding won't work, anyway, because when I do the adding it gets off by one hour in the other direction. Maybe there's a better approach to the whole problem....

更多信息:我将采用偏移量并将其添加到数据库中的其他日期时间,并且我只希望更改日期部分,而不是时间部分。无论如何,结果舍入将无效,因为当我进行添加时,它会在另一个方向上下一个小时。也许对整个问题有一个更好的方法....

4 个解决方案

#1


you could use http://ca3.php.net/date_default_timezone_set to set the timezone to GMT so there will be no offset.

您可以使用http://ca3.php.net/date_default_timezone_set将时区设置为GMT,这样就不会有偏移量。

Alternately, you can manually add an offset using the date('I',$timetamp)

或者,您可以使用日期手动添加偏移量('I',$ timetamp)

if ( date("I") == 1 ) {      // "WE ARE MDT";  
        $timeZone = "MDT";  
} else {  
        $timeZone = "MST";  
}  

#2


Force the dates to live into a timezone without Daylight Savings Time, GMT/UTC:

在没有夏令时,GMT / UTC的情况下强制日期进入时区:

function date_diff($old_date, $new_date) {
  $offset = strtotime($new_date . " UTC") - strtotime($old_date . " UTC");
  return $offset/60/60/24;
}

echo date_diff('3/15/09', '3/18/09'); // 3
echo date_diff('11/15/08', '3/18/09'); // 123

#3


You can force rounding in a specific direction by using floor() or ceil().

您可以使用floor()或ceil()强制在特定方向上进行舍入。

#4


I tried the 'UTC' code above. Didnt work for me. I stll got decimal values.

我尝试了上面的'UTC'代码。没有为我工作。我得到十进制值。

When there is daylight saving date within the date range the difference serial decimal portion will be either under .5 or over. when date range has day light saving going on 3/15 the decimal value is > .5 when daylight is going off date decimal < .5. so I just put the difference serial in a round() function and I get the whole numbers i need for number of days.

当日期范围内有夏令时时,差异序列小数部分将低于.5或更高。当日期范围在3月15日进行日光节约时,十进制值> .5当日光消失日期十进制<.5。所以我只是把差异序列放在round()函数中,我得到了我需要的整数天数。

#1


you could use http://ca3.php.net/date_default_timezone_set to set the timezone to GMT so there will be no offset.

您可以使用http://ca3.php.net/date_default_timezone_set将时区设置为GMT,这样就不会有偏移量。

Alternately, you can manually add an offset using the date('I',$timetamp)

或者,您可以使用日期手动添加偏移量('I',$ timetamp)

if ( date("I") == 1 ) {      // "WE ARE MDT";  
        $timeZone = "MDT";  
} else {  
        $timeZone = "MST";  
}  

#2


Force the dates to live into a timezone without Daylight Savings Time, GMT/UTC:

在没有夏令时,GMT / UTC的情况下强制日期进入时区:

function date_diff($old_date, $new_date) {
  $offset = strtotime($new_date . " UTC") - strtotime($old_date . " UTC");
  return $offset/60/60/24;
}

echo date_diff('3/15/09', '3/18/09'); // 3
echo date_diff('11/15/08', '3/18/09'); // 123

#3


You can force rounding in a specific direction by using floor() or ceil().

您可以使用floor()或ceil()强制在特定方向上进行舍入。

#4


I tried the 'UTC' code above. Didnt work for me. I stll got decimal values.

我尝试了上面的'UTC'代码。没有为我工作。我得到十进制值。

When there is daylight saving date within the date range the difference serial decimal portion will be either under .5 or over. when date range has day light saving going on 3/15 the decimal value is > .5 when daylight is going off date decimal < .5. so I just put the difference serial in a round() function and I get the whole numbers i need for number of days.

当日期范围内有夏令时时,差异序列小数部分将低于.5或更高。当日期范围在3月15日进行日光节约时,十进制值> .5当日光消失日期十进制<.5。所以我只是把差异序列放在round()函数中,我得到了我需要的整数天数。