-1

重複の可能性:
日付が特定の範囲内にあるかどうかを確認する方法は?

各年を次のような期間に分割する

from 11-28 to 01-20
from 01-21 to 02-16
from 02-17 to 03-15
from 03-16 to 05-04
from 05-05 to 07-25
from 07-26 to 10-10
from 10-11 to 11-27

そして、いくつかの日

$date1 = '06-09-1990';
$date2 = '05-03-1867';
$date3 = '02-29-1945';
$date4 = '06-24-2012';
$date5 = '12-25-2015';
$date6 = '07-15-2010';

日がこれらの範囲のいずれかにあるかどうかを確認する方法は?

PS私の英語ですみません

4

2 に答える 2

4

この小さな関数を使用できます:

<?php
    $s_date = strtotime("2009-03-01 12:00");
    $e_date = strtotime("2009-03-03 14:00");
    $date = strtotime("2009-03-02 13:00");
    if($date > $s_date && $date < $e_date)
        print "Date is between start and end";
    else
        print "Date is outside start and end";
?>

別の方法


function isDateBetween($dt_start, $dt_check, $dt_end){
    if(strtotime($dt_check) > strtotime($dt_start) && strtotime($dt_check) < strtotime($dt_end))
        return true;
    return false;
}
isDateBetween("2004-01-01", "2004-01-02", "2004-01-03")
于 2012-06-23T17:22:41.790 に答える
2

PHP の日付値は UNIX タイムスタンプとして表現され、1970 年 1 月 1 日から経過した秒数を表します。

mktime関数を使用して日付を作成できます ( http://php.net/manual/en/function.mktime.php )。数字があれば簡単に比較できます。

$interval_start = mktime(0, 0, 0, 1, 0, 2000);
$interval_end = mktime(0, 0, 0, 1, 0, 2010);
$my_date = mktime(0, 0, 0, 1, 0, 2004);
if($my_date > $interval_start && $my_date < $interval_end) {
    // in the interval
} else {
    // not in the interval
}
于 2012-06-23T17:22:41.190 に答える