Check if a date is in the past or the future


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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; }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.