简体   繁体   English

如何检查从日期到日期之间的两个日期

[英]how to check two dates between from-date and to-date

I have two dates from-date & to-date . 我有两个约会from-dateto-date

I have to compare them from existing dates shown below, whether any of the day fall between them or not using php? 我必须将它们与下面显示的现有日期进行比较,是否有任何一天落在他们之间或不使用PHP?
i can do for single date checking ,but i am confuse for the two date checking. 我可以做单日期检查,但我很困惑两个日期检查。

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. 示例:我必须检查这些日期: - >从15 March 2013 15 April 2013以下日期之间是否有任何天数介于这两个日期之间。

following data from db table 跟踪数据库表中的数据

 #       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.. 请告诉我有关此问题的任何逻辑或建议。

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. 因为unix时间戳是一个整数,所以你可以简单地使用关系运算符来比较它们。

Example

// 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;

EDIT 1 编辑1

To actually answer your question: 要真正回答你的问题:

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;

Please try the code, 请试试代码,

    $ourdate = strtotime('1 April 2013'); // Your date which is to be checked

    $from = strtotime('15 March 2013'); // From date
    $to = strtotime('15 April 2013'); // To date

    if ($ourdate >= $from && $ourdate <= $to)
    {
      echo "Date falls";
    }
    else
    {
    echo "No Date falls";
    }

If you need to check several dates, pass it as an array, like below... 如果您需要检查几个日期,请将其作为数组传递,如下所示...

   $i=0;
   $dates= array("11 April 2013","16 April 2013");
   foreach($dates as $ourdates)
   {
    $ourdate= strtotime($ourdates); //Your dates to be checked
    $from = strtotime('15 March 2013'); // From date
    $to = strtotime('15 April 2013'); // To date

          if ($ourdate >= $from && $ourdate <= $to)
              {
                 $i++;
              }
    }
    if($i>0)
    {
    echo "Date falls";
    }
    else
    {
    echo "No Date falls";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM