Solve PHP how to get yesterday's date
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! ๐
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022