PHPで2つの日付を比較する方法


108

日付が形式である場合はPHPで二つの日付を比較する方法'03_01_12''31_12_11'

私はこのコードを使用しています:

$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
   echo '1 is small ='.strtotime($date1).','.$date1;
else
   echo '2 is small ='.strtotime($date2).','.$date2;

しかし、それは機能しません。



可能性のある重複比較日- PHP
ウェズリーマーチ

回答:


42

日付が有効な日付オブジェクトであることを確認する必要があります。

これを試して:

$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));

次に、strtotime()メソッドを実行して違いを取得できます。


2
なぜstr_replaceをしないのですか?
self.name

36

DateTime :: createFromFormatを使用する:

$format = "d_m_y";
$date1  = \DateTime::createFromFormat($format, "03_01_12");
$date2  = \DateTime::createFromFormat($format, "31_12_11");

var_dump($date1 > $date2);

1
d#m#Yを使用する場合、サポートされているセパレーター;, :, (, ), /, ., ,, -はどれでも機能します。
Rijk

9

date_diff()関数は、2つのDateTimeオブジェクトの差を返します。

最初の日付が2番目の日付より前の場合、正の日数が返されます。それ以外の場合、負の日数:

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

出力は「+272日」になります。

$ date1 = "2014-03-15"の変更

 <?php
    $date1=date_create("2014-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    ?>

出力は「-93日」になります


8
<?php
       $expiry_date = "2017-12-31 00:00:00"
       $today = date('d-m-Y',time()); 
       $exp = date('d-m-Y',strtotime($expiry_date));
       $expDate =  date_create($exp);
       $todayDate = date_create($today);
       $diff =  date_diff($todayDate, $expDate);
       if($diff->format("%R%a")>0){
             echo "active";
       }else{
           echo "inactive";
       }
       echo "Remaining Days ".$diff->format("%R%a days");
?>

説明を追加していただけますか?
Prafulla Kumar Sahu、2015年

8

OPの実際の問題に答えるのではなく、タイトルだけに答えます。これは「phpで日付を比較する」のトップ結果なので。

日時オブジェクト(php >= 5.3.0)を使用して直接比較するのはかなり簡単

$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);

クラスDateTimeのオブジェクトを文字列に変換できませんでした
Shindo Hikaru


4

あなたは次のようなことを試すことができます:

        $date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
        $date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
        $dateDiff = date_diff($date1, $date2);
        var_dump($dateDiff);

その後、この$ dateDiff-> dのように、日数の違いにアクセスできます。


おい!おかげで、 ']'文字に少しタイプミスがありましたよね?Enterキーに近いので、私も押したに違いありません。見てくれてありがとう。
フレデリックG.サンダロ2014

3

あなたが何が問題なのかわからないが:

function date_compare($d1, $d2)
{
    $d1 = explode('_', $d1);
    $d2 = explode('_', $d2);

    $d1 = array_reverse($d1);
    $d2 = array_reverse($d2);

    if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
    {
        return $d2;
    }
    else
    {
        return $d1;
    }
}

3

これを試して

$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");

if($data1 < $data2){
    return "The most current date is date1";
}

return "The most current date is date2";


2

私はこれが遅いことを知っていますが、将来の参考のために、str_replaceを使用して日付形式を認識できる形式にすると、関数が機能します。(アンダースコアをダッシュ​​に置き換えます)

//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));

//compare the dates
if($date1 < $date2){
   //convert the date back to underscore format if needed when printing it out.
   echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
   echo '2 is small='.$date2.','.date('d_m_y',$date2);
}

2

整数に変換して比較できます。

例えば。:

$date_1 = date('Ymd');
$date_2 = '31_12_2011';

$date_2 = (int) implode(array_reverse(explode("_", $date_2)));

echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';

1

これはとてもシンプルな機能だと思います

function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
    $crtDate = new DateTime($currentDate);
    $termDate = new DateTime($terminationdate);
    if($crtDate >= $termDate)
    {
        return true;
    } else {
    return false;
    }
}

0

みんなそんなに複雑にしないでください

$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
   echo '1 is small ='.strtotime($date1).','.$date1;
else
   echo '2 is small ='.strtotime($date2).','.$date2;

私はあなたのコードにさらに2行追加しました


-1

両方の日付が同じ形式の場合は、比較演算子を使用します。

$date1 = "2018-05-05"; 
$date2 = "2019-08-19"; 

//comparison operator to  

if ($date1 > $date2) {
    echo "$date1 is latest than $date2"; 
    }
else{
    echo "$date1 is older than $date2"; 
    }

出力:2018-05-05は2019-08-19より古い


これは、日付がISO 8601形式の場合にのみ機能します。
ダーマン
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.