Solve PHP how to get yesterday's date

Photo from Unsplash

To get yesterday’s date using PHP, you need to call the strtotime() function and pass yesterday as its argument.

$date = strtotime("yesterday");

The strtotime() function returns a Unix timestamp value from an English datetime description.

Here’s an example of using the strtotime() function to get yesterday’s date:

<?php
// 👇 get yesterday's date
$date1 = strtotime("yesterday");
// or
$date2 = strtotime("-1 day");

// 👇 use date() to create a string from the date

print date("D M Y", $date1). "\n"; // Wed Sep 2022
print date("d m y", $date2); // 21 09 22

Because strtotime() returns a Unix timestamp value, you need to convert the value to a date string using the date() function.

The date() function also gives you an opportunity to format the date string as shown in the above example.

Now you’ve learned how to get yesterday’s date using PHP. Sweet! 😉

Get 98 PHP Snippets Book for FREE 🔥

98 PHP snippets that you can use in various scenarios

Save 1000+ hours of research and 10x your productivity