This is a short guide on how to get tomorrow’s date using PHP. Essentially, we want to get the next date after the current one. Fortunately, this is pretty easy to do with PHP.
Let’s start off with an example using the strtotime function!
Getting tomorrow’s date using the strtotime function.
In most cases, PHP’s strtotime function will understand the textual description of the date that you want. As a result, you can simply pass in “+1 day” and the function will return the Unix timestamp for tomorrow’s date.
//Get the unix timestamp of tomorrow's //date using strtotime $tomorrowUnix = strtotime("+1 day"); //Format the timestamp into a date string $tomorrowDate = date("Y-m-d", $tomorrowUnix); //Print it out echo $tomorrowDate;
In the snippet above, we retrieved the Unix timestamp for tomorrow’s date. After that, we converted the timestamp into a date string.
Note that you can also pass the word “tomorrow” into the strtotime function:
//Tomorrow's timestamp $timestamp = strtotime("tomorrow"); //Print it out echo date("Y-m-d", $timestamp);
Essentially, “tomorrow” and “+1 day” are the exact same thing. Both will result in strtotime returning a timestamp representing the next day.
Getting tomorrow’s date using PHP’s DateTime object.
If you’re looking for an object-orientated approach to this problem, then you can use the DateTime object.
//Get the current date and time. $date = new DateTime(); //Create a DateInterval object with P1D. $interval = new DateInterval('P1D'); //Add a day onto the current date. $date->add($interval); //Print out tomorrow's date. echo $date->format("Y-m-d");
Firstly, we created a DateTime object representing today’s date. Secondly, we created a DateInterval object with “P1D” as the period. In plain English, P1D means a “period of 1 day”.
Finally, we added our interval onto the current date.
And that’s it! Hopefully, this tutorial helped you to solve your problem. For more information on this subject, you should check out my guide on adding days to a date using PHP.
Related: Get next year’s date using PHP.