how to get yesterday’s date in PHP

Question

Answers ( 1 )

    0
    2024-01-11T18:05:23+00:00

    In PHP, you can get yesterday's date using the date() function combined with the strtotime() function. The date() function is used to format a date and time, while strtotime() is used to convert an English textual datetime description into a Unix timestamp.

    Here's an example of how you can do this:

    <?php
    // Get yesterday's date
    $yesterday = date("Y-m-d", strtotime("-1 day"));
    
    // Display the date
    echo $yesterday;
    ?>
    

    In this example, strtotime("-1 day") computes the timestamp for one day before the current time, and date("Y-m-d") formats this timestamp into a readable date in the format of "Year-Month-Day".

    This method is straightforward and is commonly used for such date manipulations in PHP.

Leave an answer