How to Get Next Date in PHP
During programming, we might be required to get tomorrow's date or the next date for any given date. We will go through several methods to cover the current topic of how to get the next date in PHP. Stay tuned!
But before going to determine tomorrow or next Nth date it is recommended to check PHP date/time functions and their usage.We have explained some DateTime functions definitions and examples which we will use here. Letís dive into the following step by step approach to learn How to Get Next Date in PHP with examples:
Scroll for More Useful Information and Relevant FAQs
PHP add 1 day to current date
echo date('j F, Y',strtotime("+1 days"));
Result: 31 January, 2022
Note: Current Server Date was: 30 January 2022 when calculating tomorrow using above code
- strtotime converts English text "+1 days" into a corresponding Unix timestamp.
- Timestamp received from strtotime function further passed to date function to get next date in given format j F, Y
Find Next Date for any Given date using strtotime function
$givenDate = "2022-01-31"; $timestampForGivenDate = strtotime ( $givenDate ); $englishText = '+1 day'; $requireDateFormat = "Y-m-d"; echo date($requireDateFormat,strtotime ( $englishText , $timestampForGivenDate )) ;
- Letís say the We have given 2022-01-31
- First converted given date into a timestamp and stored in $timestampForGivenDate
- English text +1 day is applied here to find the next day.
- Described here to get a date in Y-m-d format. You may change the date format as per your programming needs.
- Now we have a timestamp for a given date stored in $timestampForGivenDate. Next, we need a timestamp for the next day. Therefore
strtotime ( $englishText , $timestampForGivenDate )used to get a timestamp for the next date.
- Further passed next date timestamp to date function with expected date format Y-m-d. Thatís it.
Get next 7 day date
We can follow the same way as per the previous example to find the next 7th day's date or the Next Nth day's date.
To find Next 7th Date from today:
echo date('j F, Y',strtotime("+7 days"));
Just use English text "+7 days" to get the timestamp and pass to date function as demonstrated above
To find the Next 7th date from the given date.
$givenDate = "2022-01-31"; $timestampForGivenDate = strtotime ( $givenDate ); $englishText7 = '+7 day'; $requireDateFormat = "j F, Y"; echo date($requireDateFormat,strtotime ( $englishText7 , $timestampForGivenDate )) ;
Result: 7 February, 2022
Get the previous date in PHP
We have already covered calculating the date minus 1 day from today or for any given date. Please visit here to learn more.