【问题标题】:Figuring out that today is x number of days is after given date in PHP找出今天是 PHP 中给定日期之后的 x 天数
【发布时间】:2011-09-13 17:21:22
【问题描述】:

我的数据库中有数据返回事件的月/日/年数据点。

我想要做的是检查今天是否是我得到的月/日/年之后的 20 天。

到目前为止,我有类似的东西:

// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];

// Get today's date
$todays_date = date("Y-m-d");

// Create the date I am comparing with 
$date_string = $year.'-'.$month.'-'.$day;

我的问题是如何比较它?在比较两个日期时,格式是否重要?

谢谢!

【问题讨论】:

    标签: php date


    【解决方案1】:

    将月/日/年解析为日期时间:

    $other = date_create($year.'-'.$month.'-'.$day);
    

    增加 20 天:

    $twenty_days_after = clone $other;
    $twenty_days_after->modify('+20 days');
    

    与今天比较:

    if ($today >= $twenty_days_after) {
        // today is 20 days after the month/day/year that you got
    }
    

    date_create

    【讨论】:

    【解决方案2】:
    $database_date             = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
    $twenty_days_from_now      = strtotime('+20 days');
    $twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));
    

    使用strttime 是最简单的方法。

    【讨论】:

      【解决方案3】:

      使用

      strtotime()
      

      将您的日期转换为时间戳的功能,您可以更轻松地比较它们

      【讨论】:

        【解决方案4】:
        // Get date data
        $day = $row['DAYOFMONTH(hike_date)'];
        $year = $row['YEAR(hike_date)'];
        $month = $row['MONTH(hike_date)'];
        
        // Get today's date
        $todays_date = date("Y-m-d");
        
        // Create the date I am comparing with 
        $date_string = $year.'-'.$month.'-'.$day;
        
        if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
            //date is 20 days before today
        }
        

        strtotime 做神奇的事

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多