PHP Comparing Date and Time Values

In my PHP application, I am trying to compare date time values, for example:

if($datetime_from_db < date('Y-m-d H:i:s'))
{
    // then do something
}

Both values ​​are in the same format. I cannot understand why it only compares the date and ignores the time. Date and time values ​​are important to me, but I don’t know how to make it work.

+3
source share
3 answers

Comparing a string as "2011-02-14 15:46:00"with another stringactually does not compare dates , it compares two lines by line, analyzing numerical rules . You will need to compare the actual numerical timestamps:

strtotime($datetime_from_db) < time()
+18
source

, 2038 , strtotime() time().

. .

:

new DateTime($datetime_from_db) < new DateTime();

+4

.

$today = date("m-d-Y H:i:s");
$thisMonth =date("m");
$thisYear = date("y");
$expectedDate = $thisMonth."-08-$thisYear 23:58:00";
//pr($today);
//pr($expectedDate);


    if (strtotime($expectedDate) > strtotime($today)) {
        echo "Expected date is greater then current date";
        return ;
    } else
        {
         echo "Expected date is lesser then current date";
        }
0

Source: https://habr.com/ru/post/1791927/


All Articles