Solve PHP how to get yesterday's date

Posted on Sep 22, 2022

Learn how you can get yesterday's date in PHP - with code examples

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! 😉

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.