how to get yesterday’s date in PHP


Answers ( 1 )


    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:

    // 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