如何检查从日期到最新的两个日期 [英] how to check two dates between from-date and to-date
问题描述
我有两个日期 from-date
& 至今
。
I have two dates from-date
& to-date
.
我必须将它们与现在显示的日期进行比较,他们还是不使用php?
i可以做单日期检查,但我很困惑两个日期检查。
I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking.
示例:
我必须检查这些日期: - > from = 2013年3月15日
& 2013年4月15日
之间的日期是否在这两个日期之间。
Example:
i have to check these dates:-> from=15 March 2013
& 15 April 2013
between the following dates whether any days falls in between these two date or not.
# from date to-date
-----------------------------------------
1 01 April 2013 30 April 2013 //here we will find as falling
2 01 May 2013 15 May 2013
3 01 June 2013 20 June 2013
目前,在我看来,甚至没有一个逻辑来尝试。请给我关于这个问题的任何逻辑或建议。
Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..
推荐答案
比较日期的最简单的方法是将它们转换为< a href =http://en.m.wikipedia.org/wiki/Unix_time =nofollow> unix时间戳
The simplest way to compare dates is to convert them to a unix timestamp
因为unix时间戳是一个整数,你可以简单地使用关系运算符来比较它们。
Because the unix timestamp is an integer, you can simply use relational operators to compare them.
示例
// set some example data
$referenceDate = '01 April 2013';
$fromDate = '01 January 2013';
$toDate = '01 June 2013';
// convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );
// isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
编辑1
要真正回答您的问题:
您有两个范围需要测试重叠,这个问题已在这里回答测试两个整数范围的最有效的方法是重叠?
You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?
// our two ranges overlap if the following conditions are met
$dateRangeOverlaps = $referenceFromTimestamp <= $toTimestamp and $fromTimestamp <= $referenceToTimestamp;
这篇关于如何检查从日期到最新的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!