Posted By

gromitski on 12/14/13


Tagged

php time date compare comparison


Versions (?)

Check if a date is in the past or the future


 / Published in: PHP
 

URL: http://gromitski.com/blog/checking-if-a-date-is-in-the-past-or-the-future-php/

Examine a date and return false if it's in the past, or true for the future.

Slightly more complicated than it needs to be, however there's an easy tutorial with full explanation if you follow the attached link.

  1. function checkDateAndTime($day,$month,$year,$hour,$minute) {
  2. $dateForConverting = $day."-".$month."-".$year."@".$hour.":".$minute;
  3. $formattedDate = date_create_from_format("j-n-Y@G:i", $dateForConverting);
  4. $newDate = date_format($formattedDate, "Y-m-d H:i:s");
  5. $now = date("Y-m-d H:i:s", time());
  6. $newDateInSecs = strtotime($newDate);
  7. $nowInSecs = strtotime($now);
  8. $returnBool = true;
  9. $diffInSecs = $newDateInSecs - $nowInSecs;
  10. if($diffInSecs < 0) { $returnBool = false; }
  11. return $returnBool; }

Report this snippet  

You need to login to post a comment.