在php中,将两个日期比较为字符串格式(Y-m-dh:i:s)和两个日期比较为strotime of date有什么区别?

在php中,将两个日期比较为字符串格式(Y-m-dh:i:s)和两个日期比较为strotime of date有什么区别?,php,if-statement,Php,If Statement,我混淆了两个不同的条件,但给出了相同的结果 第一例 输出 未过期 第二种情况 输出 未过期 所以在上面两个例子中,有一个区别是,在第一种情况下,我将正常日期作为字符串进行比较,但在第二种情况下,我将日期与strtime函数进行比较,我知道第二种方法适合检查 但是我想知道为什么第一个案例不合适,或者你能解释一下哪种类型的案例,或者第一个案例给出了不合适结果的日期 此外,这两种类型的案例是否都会在任何案例的日期时间比较中产生问题?$row\u otp\u time='2020-10-20 12:5

我混淆了两个不同的条件,但给出了相同的结果

第一例

输出
未过期


第二种情况

输出
未过期

所以在上面两个例子中,有一个区别是,在第一种情况下,我将正常日期作为字符串进行比较,但在第二种情况下,我将日期与strtime函数进行比较,我知道第二种方法适合检查

但是我想知道为什么第一个案例不合适,或者你能解释一下哪种类型的案例,或者第一个案例给出了不合适结果的日期


此外,这两种类型的案例是否都会在任何案例的日期时间比较中产生问题?

$row\u otp\u time='2020-10-20 12:52:00'

  $date = new DateTime($row_otp_time);
  $date->modify('+5 minute');
  $timezone = date_default_timezone_get();
  date_default_timezone_set($timezone);
 
  $current_date_time = '2020-10-20 01:00:00'; // date('Y-m-d h:i:s')

  $expired_datetime = $date->format("Y-m-d h:i:s");

  if(strtotime($current_date_time) > strtotime($expired_datetime)){
    echo 'exxpired';
  }
  else{
    echo 'not expired';
  }

如果您得到的是当前日期时间,比如带有小“h”的日期('Y-m-d h:i:s')(注释代码),那么如果am/pm正在更改,则不会正确给出比较日期字符串。

基于日期字符串的第一次比较,第二个基于数字。你为什么期望不同的结果?@DevsiOdedra我知道,但我用更具体的方式编辑了我的问题细节。我想知道第一个案例给出的结果对于某个特定日期是不正确的。当您需要比较时,数字比字符串好。使用第二个。@MagnusEriksson如果
IF
条件通过
PHP
如何理解字符串的日期类型?表示它是普通字符串,那么为什么PHP会给出比较日期字符串的正确结果。
  $row_otp_time = '2020-10-20 01:59:31';

  $date = new DateTime($row_otp_time);
  $date->modify('+5 minute');
  $timezone = date_default_timezone_get();
  date_default_timezone_set($timezone);
  $expired_datetime = $date->format("Y-m-d h:i:s");

  $current_date_time = '2020-10-20 02:04:31'; //current date      

  if(strtotime($current_date_time) > strtotime($expired_datetime)){
    echo 'expired';
  }
  else{
    echo 'not expired';
  }
  $date = new DateTime($row_otp_time);
  $date->modify('+5 minute');
  $timezone = date_default_timezone_get();
  date_default_timezone_set($timezone);
 
  $current_date_time = '2020-10-20 01:00:00'; // date('Y-m-d h:i:s')

  $expired_datetime = $date->format("Y-m-d h:i:s");

  if(strtotime($current_date_time) > strtotime($expired_datetime)){
    echo 'exxpired';
  }
  else{
    echo 'not expired';
  }