Check if a date is today, yesterday or tomorrow

date is today
PHP

Check if a date is today, yesterday or tomorrow

How to check a given date is today, yesterday or tomorrow, or how many days far,etc using php ?

Sometime we may have to find a given date is today, yesterday or tomorrow, or how many days far,etc. In this tutorial we’ll discuss how to find the given date is today yesterday or tomorrow,etc.

To find this a simple solution is there as follows;

<?php
    $date = "2020-08-31";
    $curr_date=strtotime(date("Y-m-d"));
    $the_date=strtotime($date);
    $diff=floor(($curr_date-$the_date)/(60*60*24));
    switch($diff)
    {
        case 0:
            echo "Today";
            break;
        case 1:
            echo "Yesterday";
            break;
        default:
            echo $diff." Days ago";
    }
?>

Explanation :

  1. Define the date which you want to check or else you can declare a function and pass the date which you want to check as a parameter.
  2. Get the current date because we are comparing the input date with the current date to get the result.
  3. Pass the input date to strtotime() function,
    1. strtotime — Parse about any English textual datetime description into a Unix timestamp
  4. floor(($curr_date-$the_date)/(60 * 60 * 24)) will return the no of differences between the current date and input date.
  5. Use a switch case to display the result.

For other Php solutions please visit PHP .

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top