Understanding PHP Date and Time
In this article, we will examine PHP dates and times closely with examples. We will cover topics such as working with timezones, manipulating dates and times, and creating and formatting dates.
No matter what you need for formatting dates, calculating intervals between dates, displaying the current date, or displaying dates in different formats, PHP gives you all the features you need.
This post will help you build fast, reliable date and time functionality into your web applications using PHP date and time handling.
PHP Date() Function
With PHP date() function, you have the ability to format dates and times in multiple formats.
There are two arguments that are passed to the function:
- The format string and the optional timestamp. In the format string, formatting characters are used to specify how the date should be formatted.
- Timestamps are optional Unix timestamps that represent the date and time to format.
|format||This is required. The format of the timestamp should be specified|
|timestamp||It is optional. Provide a timestamp for the date. As a default, PHP date and time are set to the current date and time|
The term “timestamp” refers to a sequence of characters that denote the date and/or time when an event took place.
PHP Date() functions require a format parameter that specifies how to format the date (or time) when it is passed to them.
The following are some common characters that are used as part of a date:
|d||This value represents the day of the month (from 1st to 31st).|
|m||This is a representation of a month (01-12).|
|Y||Represents an entire year (in four digits).|
|l||(lowercase ‘L‘) – Indicates the day of the week that it is.|
There are also other characters that can be inserted between the characters for adding additional formatting, such as / ., or –.
In the example below, we show three different ways in which we can format today’s date:
In below example, let’s try different formats of dates using the date() function:
Get Automatic Copyright Year
The following code is an example that can be used to automatically update the copyright year on your website using the date() function:
The following are some common characters that are used to indicate the time of day:
|H||Format of 24-hours of an hour (from 00 to 23).|
|h||The format in which a 12-hour period is expressed with a leading zero (01 to 12).|
|i||This represents the minutes of the hour with leading zeros (00 to 59).|
|s||This is the number of seconds with leading zeros (00-09).|
|a||A lowercase ante meridiem and post meridiem (the time on the clock).|
Following is an example of how the current time is outputted in the format specified below:
Below is an example of current time in 12 hour format:
Now Get Time Zone
If you are getting an incorrect time back from the code, it may be because your server is located in another country or set up for a different time zone than what you expected.
So, if you want the time to be accurate based on the actual location of the user, you can set the timezone to correspond with that location.
There is an example below that sets the timezone to “America/New_York“, and then outputs the current time in the format specified:
Date Through mktime()
PHP Date() function accept a timestamp as an optional parameter.
As in the examples above, ignoring this field will use the current date and time.
A date can be converted into a Unix timestamp using PHP mktime() function.
It is a simple method of representing the number of seconds that have passed since the Unix Epoch (January 1, 1970 00:00:00 GMT) and the time indicated.
mktime(hour, minute, second, month, day, year)
With the date() function and the mktime() function, the following date and time are created:
Date With strtotime()
With the help of PHP strtotime(), you can convert a string of human-readable date into a Unix timestamp (which indicates the number of seconds since (January 1, 1970 00:00:00 GMT).
Here is an example of how to create a date and time using the strtotime() function:
Despite this, strtotime() isn’t a perfect function, so it’s best to check the strings before inserting them into it.
Below is an example that outputs PHP dates for the next six Sundays, as follows:
1 – The first line of code initializes a variable called $start-date with the value of the next upcoming Sunday, using the strtotime function to convert the string “Sunday” to a Unix timestamp.
2 – The second line of code initializes a variable called $end-date with a Unix timestamp that represents the date 6 weeks after $start-date.
- This is accomplished using the strtotime function again, this time passing two arguments:
- the first argument is a string representing a time period to add (in this case, “+6 weeks“).
- The second argument is the starting Unix timestamp ($start-date).
- The while loop iterates as long as $start-date is less than $end-date. In each iteration, it outputs the date represented by the Unix timestamp $start-date, formatted as a string using the date function.
- The format used here is “M d“, which displays the abbreviated month name followed by the day of the month (e.g. “Mar 01“).
3 – After each iteration of the loop, the $startdate variable is updated to represent the date one week later than the current value of $start-date.
- This is done using the strtotime function again, this time adding “+1 week” to the current value of $start-date.
- This ensures that the loop will iterate through each Sunday for the next 6 weeks.