查找两个unix时间戳之间的日期存在问题 [英] Problems with finding the days between two unix timestamps

查看:155
本文介绍了查找两个unix时间戳之间的日期存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道很多问题和答案,但是不幸的是,我认为我的情况可能与众不同?由于某种原因,时间的变化似乎使计算的日期比应该计算的要少一天。

I know there a lot of questions and answers of this already, but unfortunately I think my situation may be unique? For some reason, the time-change seems to be making the day calculate as one day less than it should be calculating.

这是我使用的PHP,它是一直很好,直到开始分别与夏令时之前和夏令时之前的开始和结束日期重叠(仅供参考,这是一个新问题,因为夏令时开始于本周末!):

Here is my PHP that I was using and it was working great, until it started to overlap a start and end date that was BEFORE daylight savings, and AFTER daylight savings, respectively (FYI, this is a recent issue, since daylight savings time starts this weekend!):

//$lastDate and $firstDate are 2 unix timestamps with valid month, day, and year values.
//The times are irrelevant at this point, they are only meant to represent a day.

//I start by making sure these have the same time values.
$lastDate = mktime(23, 59, 59, date("m", $lastDate), date("j", $lastDate), date("Y", $lastDate));
$firstDate = mktime(23, 59, 59, date("m", $firstDate), date("j", $firstDate), date("Y", $firstDate));

//Then calculate the total number of days in between.
$totalDays = abs(floor(($firstDate - $lastDate)/(60*60*24)));

同样,要清楚一点,如果我不重叠夏令时,上述方法也适用...

So again, to be clear, the above works if i'm not overlapping the daylight savings time change...

供参考:

如何查找两个指定日期之间的日期?

PHP中两个unix时间戳之间的实际天数

找到介于2天之间的天数php中的unix时间戳

而且,我现在仍在使用PHP 5.2。

And, I'm still on PHP 5.2 right now.

编辑/更新:

我在此链接上找到了以下内容:

EDIT/UPDATE:
I've found the following on this links:

如何查找两个指定日期之间的日期?

如何计算不用86400除以php的2个unix时间戳之间的时间间隔(60400 * 60 * 24)

//$lastDate and $firstDate are 2 unix timestamps with valid month, day, and year values.
//The times are irrelevant at this point, they are only meant to represent a day.

//I start by making sure these have the same time values.
$lastDate = mktime(10, 00, 00, date("m", $lastDate), date("j", $lastDate), date("Y", $lastDate));
$firstDate = mktime(10, 00, 00, date("m", $firstDate), date("j", $firstDate), date("Y", $firstDate));

//Then calculate the total number of days in between. 
$totalDays = floor($firstDate / 86400) - floor($lastDate/ 86400);

对于DST的交叉,它现在似乎可以正常工作。有人看到这个问题吗?

And it seems to work right now for the crossover of DST. Anyone see any issues with this?

推荐答案

您是否在PHP 5.3+上运行? DateInterval 非常适合这个问题。

http://www.php.net/manual/en/datetime.diff.php

Are you running on PHP 5.3+? A DateInterval fits the problem nicely.
http://www.php.net/manual/en/datetime.diff.php

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

这篇关于查找两个unix时间戳之间的日期存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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