- PHP Date and Time
- The PHP Date() Function
- Syntax
- Get a Date
- Example
- PHP Tip — Automatic Copyright Year
- Example
- Get a Time
- Example
- Get Your Time Zone
- Example
- Create a Date With mktime()
- Syntax
- Example
- Create a Date From a String With strtotime()
- Syntax
- Example
- Example
- More Date Examples
- Example
- Example
- Complete PHP Date Reference
- How to Get Next, Previous Day, Month, Year From Date In PHP
- How to Get Next, Previous Day, Month And Year From Date In PHP
- How to Get Previous Day From Date In Php?
- How to Get Next Day From Date In Php?
- How to Get Previous Month From Date In Php?
- How to Get Next Month From Date In Php?
- How to Get Previous Year From Date In Php?
- How to Get Next Year From Date In Php?
- How to Get PHP Date + 1 Day
- Scroll for More Useful Information and Relevant FAQs
- PHP add 1 day to current date
- Find Next Date for any Given date using strtotime function
- Get next 7 day date
- To find Next 7th Date from today:
- Find below code in php add days to date to find Next 7th date
- Get the previous date in PHP
- How to Get PHP Date + 1 Day
- Was this post helpful?
PHP Date and Time
The PHP date() function is used to format a date and/or a time.
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
Syntax
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Get a Date
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
- d — Represents the day of the month (01 to 31)
- m — Represents a month (01 to 12)
- Y — Represents a year (in four digits)
- l (lowercase ‘L’) — Represents the day of the week
Other characters, like»/», «.», or «-» can also be inserted between the characters to add additional formatting.
The example below formats today’s date in three different ways:
Example
echo «Today is » . date(«Y/m/d») . «
«;
echo «Today is » . date(«Y.m.d») . «
«;
echo «Today is » . date(«Y-m-d») . «
«;
echo «Today is » . date(«l»);
?>?php
PHP Tip — Automatic Copyright Year
Use the date() function to automatically update the copyright year on your website:
Example
Get a Time
Here are some characters that are commonly used for times:
- H — 24-hour format of an hour (00 to 23)
- h — 12-hour format of an hour with leading zeros (01 to 12)
- i — Minutes with leading zeros (00 to 59)
- s — Seconds with leading zeros (00 to 59)
- a — Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:
Example
Note that the PHP date() function will return the current date/time of the server!
Get Your Time Zone
If the time you got back from the code is not correct, it’s probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set the timezone you want to use.
The example below sets the timezone to «America/New_York», then outputs the current time in the specified format:
Example
Create a Date With mktime()
The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above).
The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax
The example below creates a date and time with the date() function from a number of parameters in the mktime() function:
Example
Create a Date From a String With strtotime()
The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax
The example below creates a date and time from the strtotime() function:
Example
PHP is quite clever about converting a string to a date, so you can put in various values:
Example
$d=strtotime(«next Saturday»);
echo date(«Y-m-d h:i:sa», $d) . «
«;
However, strtotime() is not perfect, so remember to check the strings you put in there.
More Date Examples
The example below outputs the dates for the next six Saturdays:
Example
$startdate = strtotime(«Saturday»);
$enddate = strtotime(«+6 weeks», $startdate);
?php
while ($startdate < $enddate) echo date("M d", $startdate) . "
«;
$startdate = strtotime(«+1 week», $startdate);
>
?>
The example below outputs the number of days until 4th of July:
Example
$d1=strtotime(«July 04»);
$d2=ceil(($d1-time())/60/60/24);
echo «There are » . $d2 .» days until 4th of July.»;
?>?php
Complete PHP Date Reference
For a complete reference of all date functions, go to our complete PHP Date Reference.
The reference contains a brief description, and examples of use, for each function!
How to Get Next, Previous Day, Month, Year From Date In PHP
How to get next and previous day, month and year from given date in PHP. In this tutorial, you will learn you how to get next and previous day, month and year from given date in php.
This tutorial will guide you with examples of how to get next and previous day, month and year from given date in php.
How to Get Next, Previous Day, Month And Year From Date In PHP
- How to Get Previous Day From Date In Php?
- How to Get Next Day From Date In Php?
- How to Get Previous Month From Date In Php?
- How to Get Next Month From Date In Php?
- How to Get Previous Year From Date In Php?
- How to Get Next Year From Date In Php?
How to Get Previous Day From Date In Php?
You can see the following example of how to get previous day from date in php:
How to Get Next Day From Date In Php?
You can see the following example of how to get next day from date in php:
How to Get Previous Month From Date In Php?
This example of how to get previous month from date in php:
How to Get Next Month From Date In Php?
You can see the following example of how to get next month from date in php:
How to Get Previous Year From Date In Php?
For find previous year from date in PHP, So, You can see the following example:
How to Get Next Year From Date In Php?
For find next year from date in PHP, So, You can see the following example:
How to Get PHP Date + 1 Day
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 PHP Date + 1 Day 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
Code Explanation:
- 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 )) ;
Code Explanation:
- 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
Find below code in php add days to date to find Next 7th date
$givenDate = "2022-01-31"; $timestampForGivenDate = strtotime ( $givenDate ); $englishText7 = '+7 day'; $requireDateFormat = "j F, Y"; echo date($requireDateFormat,strtotime ( $englishText7 , $timestampForGivenDate )) ;
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.
How to Get PHP Date + 1 Day
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:
Was this post helpful?
Feedback (optional) Please provide additional details about the selection you chose above so that we can analyze the insightful comments and ideas and take the necessary steps for this topic. Thank you