How can I compare two dates in PHP?

前端 未结 13 2017
一整个雨季
一整个雨季 2020-11-22 06:02

How can I compare two dates in PHP?

The date is stored in the database in the following format

2011-10-2

If I wanted to

相关标签:
13条回答
  • 2020-11-22 06:40
    $today = date('Y-m-d');//Y-m-d H:i:s
    $expireDate = new DateTime($row->expireDate);// From db 
    $date1=date_create($today);
    $date2=date_create($expireDate->format('Y-m-d'));
    $diff=date_diff($date1,$date2);
    
    //echo $timeDiff;
    if($diff->days >= 30){
        echo "Expired.";
    }else{
        echo "Not expired.";
    }
    
    0 讨论(0)
  • 2020-11-22 06:40

    You can convert the dates into UNIX timestamps and compare the difference between them in seconds.

    $today_date=date("Y-m-d");
    $entered_date=$_POST['date'];
    $dateTimestamp1 = strtotime($today_date);
    $dateTimestamp2 = strtotime($entered_date);
    $diff= $dateTimestamp1-$dateTimestamp2;
    //echo $diff;
    if ($diff<=0)
       {
         echo "Enter a valid date";
       }
    
    0 讨论(0)
  • 2020-11-22 06:41

    Here's my spin on how to get the difference in days between two dates with PHP. Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.

    $today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
    $wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
    $diff = $today->diff($wanted);
    $days = $diff->days;
    if (($diff->invert) != 0) $days = -1 * $days;
    $overdue = (($days < 0) ? true : false);
    print "<!-- (".(($days > 0) ? '+' : '').($days).") -->\n";
    
    0 讨论(0)
  • 2020-11-22 06:43

    Found the answer on a blog and it's as simple as:

    strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
    

    And you'll get the days between the 2 dates.

    0 讨论(0)
  • 2020-11-22 06:44

    If all your dates are posterior to the 1st of January of 1970, you could use something like:

    $today = date("Y-m-d");
    $expire = $row->expireDate; //from database
    
    $today_time = strtotime($today);
    $expire_time = strtotime($expire);
    
    if ($expire_time < $today_time) { /* do Something */ }
    

    If you are using PHP 5 >= 5.2.0, you could use the DateTime class:

    $today_dt = new DateTime($today);
    $expire_dt = new DateTime($expire);
    
    if ($expire_dt < $today_dt) { /* Do something */ }
    

    Or something along these lines.

    0 讨论(0)
  • 2020-11-22 06:45

    first of all, try to give the format you want to the current date time of your server:

    1. Obtain current date time

      $current_date = getdate();

    2. Separate date and time to manage them as you wish:

    $current_date_only = $current_date[year].'-'.$current_date[mon].'-'.$current_date[mday]; $current_time_only = $current_date['hours'].':'.$current_date['minutes'].':'.$current_date['seconds'];

    1. Compare it depending if you are using donly date or datetime in your DB:

      $today = $current_date_only.' '.$current_time_only;

      or

      $today = $current_date_only;

      if($today < $expireDate)

    hope it helps

    0 讨论(0)
提交回复
热议问题