PHP
 Computer >> コンピューター >  >> プログラミング >> PHP

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


PHPで2つの日付を比較するためのコードは、次のとおりです。ここでは、等式演算子を使用して日付を比較しました-

<?php
   $dateOne = "2019-10-30";
   $dateTwo = "2019-10-30";
   echo "Date1 = $dateOne";
   echo "\nDate2 = $dateTwo";
   if ($dateOne == $dateTwo)
      echo "\nBoth the dates are equal!";
   else
      echo "Both the dates are different!";
?>

出力

これにより、次の出力が生成されます-

Date1 = 2019-10-30
Date2 = 2019-10-30
Both the dates are equal!

別の例を見てみましょう-

<?php
   $dateOne = "2019-11-08";
   $dateTwo = "2018-08-10";
   echo "Date1 = $dateOne";
   echo "\nDate2 = $dateTwo";
   if ($dateOne < $dateTwo)
      echo "\nDateTwo is the latest date!";
   else
      echo "\nDateOne is the latest date!";
?>
をエコーし​​ます

出力

これにより、次の出力が生成されます-

Date1 = 2019-11-08
Date2 = 2018-08-10
DateOne is the latest date!

  1. PHPのnext()関数

    next()関数は、配列の内部配列ポインターを次の要素に進めます。 構文 next(arr) パラメータ 到着 −指定された配列 戻る next()関数は、成功すると、配列内の次の要素の値を返します。使用可能な要素がなくなると、FALSEが返されます。 例 以下は例です- <?php $os = array('windows', 'mac', 'linux', 'solaris'); echo current($os) . "<br>"; echo next($os); ?>

  2. PHPのend()関数

    end()関数は、配列の内部ポインタを最後の要素に設定します 構文 end(arr) パラメータ 到着 −指定された配列 戻る end()関数は、成功すると、配列の最後の要素の値を返します。配列が空の場合はFALSEを返します。 例 以下は例です- <?php $os = array('windows', 'mac', 'linux', 'solaris'); echo end($os); ?> 出力 以下は出力です- solaris 例 別の例を見てみましょう- <?php $a = arra