首页 > 解决方案 > 试着知道这个日期是今天/明天/某天前

问题描述

我想检查日期是今天、明天、昨天还是以前。但是我的代码不起作用。

代码:


    public function istimedate($timestamp=''){
          $timestamp=date_create($timestamp);
          $date = strtotime(date($timestamp,"Y-m-d"));
          $current_Time = strtotime(date("Y-m-d"));
    
          if($date == $current_Time) { 
              $day_time = 'Today';
          } elseif(strtotime("-1 day", $date) == $current_Time) {
              $day_time = 'Yesterday';
          } elseif(strtotime("+1 day", $date) == $current_Time) {
              $day_time = 'Tomorrow';
          } else {
              $day_time = 'Someday ago';
          }
          return $day_time; 
        }
    $day = $this->istimedate('2021-01-01 09:33:04');

标签: timesimpledateformatmagento-2.0

解决方案


使用正确格式转换的日期。$date = strtotime(date_format($timestamp,"Y-m-d"));安装的$date = strtotime(date($timestamp,"Y-m-d"));


推荐阅读