簡體   English   中英

時間段內的時間段(PHP)

[英]time period in time period (PHP)

我嘗試找出時間段是否在時間段內。 我有我的參考時間段和比較時間段。 讓我舉一個例子:

  • 時間段A(參考)從2014年1月1日到2014年12月1日(tt.mm.yyyy)。
  • 時間段B(比較)從1.4.2014到1.5.2014。

=>完全可以。

  • 時間段C(參考)從1.1.2014到1.3.2014
  • 時間段D(比較)從1.2.2014到1.5.2014。

=>不好,因為D在C中。

我希望你能得到我想要的。 我試圖使serval <=>采取行動,但這開始變得龐大而緩慢。 也許有更快的方法可以做到這一點。

另外,MySQL能夠做到這一點嗎?

您可以執行以下操作:

檢查Reference_start >= comparative_start && Reference_end < comparative_end ,如果此條件成立,那么您的時間將會重疊。

如果您有一個reference期間(具有startDateendDate )並且有一個comparative期間,則可以在MySQL中使用以下where子句:

where ((reference.startDate > comparative.endDate) or reference.endDate < comparative.startDate)

如果兩個周期沒有交集,則為真。

您可以使用php timestamp嘗試此操作

$reference_start_date = "1.1.2014";
$reference_end_date = "1.2.2014";

$comparative_start_date = "1.4.2014";
$comparative_end_date = "1.5.2014 ";

$reference_start_time = strtotime(str_replace(".", "-", $reference_start_date);
$reference_end_time = strtotime(str_replace(".", "-", $reference_end_date);

$comparative_start_time = strtotime(str_replace(".", "-", $comparative_start_date);
$comparative_end_time = strtotime(str_replace(".", "-", $comparative_end_date);

if($comparative_start_time>$reference_start_time && $comparative_start_time<$reference_end_time)
{
    echo "Not OK";
}
else if($comparative_end_time>$reference_start_time && $comparative_end_time<$reference_end_time)
{
    echo "Not OK";
}
else if($comparative_start_time<$reference_start_time && $comparative_end_time>$reference_end_time)
{
     echo "Not OK";
}
else
{
     echo "OK";
}

假設您輸入的日期是UTC,那么比較兩個日期范圍確實非常簡單。 可能發生5種特定情況:

11111......
......22222

..11111.....
.....22222..

...11111....
...22222....

.....11111..
..22222.....

......11111
22222......

您只尋找第一個和最后一個。 構造一個if查詢並將其取反很容易:

if (!($dateRange1End <= $dateRangeStart2 && $dateRange2End <= $dateRange1Start))
    // NOT OKAY
else
    // OKAY

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM