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 .

Related Posts

forest fire

Forest Fire – Techgig, All testcases passed. Challenge #1

Forest Fire (100 Marks) Challenge 1: Forest fire You are camping in a forest area at night. You are living with the forest officers to experience their challenges…

the magic wand

The Magic Wand – TechGig PHP answer, all test cases passed. Challenge #2

The Magic Wand – TechGig PHP answer – all testcases passed The Magic Wand (100 Marks) You are a wizard who possesses the magic wand that can be…

PHP interview Questions and Answers

What is PHP? Answer: PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP code is embedded within…

install php

A Step-by-Step Guide on How to Install PHP

Install PHP : A Step-by-Step Guide on How to Install PHP Introduction: PHP is a widely used server-side scripting language that powers countless websites and web applications….

what is php

What is PHP? Understanding PHP: The Powerhouse of Web Development

Introduction: What is PHP? Understanding PHP: The Powerhouse of Web Development: In the world of web development, PHP stands tall as one of the most widely-used and…

php form

Complete Php form example

Here we are going to check how to create a php form and insert its values to database table. In this example we will be submitting the…

Leave a Reply

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