PHP date and time functions are in-built functions to print the date and time, It could be current, today, yesterday, tomorrow, even a few years ago, or future date. You can play with date() function and other date and time creation functions and print any date/time dynamically.
date() function is used to format the date. You can create any date using the date creation function like
date_format(), etc and then make the human-readable date format using the date() function.
For example, we can use this date() function to store the current date in the database while publishing or updating any article. You can also use it to show the countdown for upcoming events or sales or anything which you want to do.
So, in this article, we will learn about the PHP date and time functions and how we can use them to print the date and time.
PHP date() Function
As I said, the PHP
date() function is used to print the human-readable format from local date and time. You just have to specify the format as a parameter in this function.
This is the syntax of the date() function and it has two parameters.
- format: It is a format that we want to print out. Like YYYY-MM-DD, MM-DD-YYY, etc.
- timestamp: It is optional parameter, default will print the current PHP date time on the server using
Let’s see a basic example of the date() function.
PHP date_create() Function
date_create() function is used to create a new date. It will return the new DateTime object and we can use this object to create a new date in any format.
It has two parameters and both are optional and the default is the current time and timezone respectively.
DateTime Object ( [date] => 2021-11-27 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC )
Now we use this object to make any format of date and print a new date format using the date_format() function. Let’s see how you can make it.
PHP date_format() Function
As I said, you can use the
date_format() function to convert the format of a date. So it will return a new date formatted according to the specified format.
You have to pass the DateTime object and format of the date that you want to print. Let’s see how we can convert the format of the above date_create() function example.
PHP date_diff() Function
Let’s suppose you want to see the difference between the two dates. To get the difference between the two dates, we will use the
date_diff() function. It will calculate the difference between two DateTime objects and print them.
Pass the two DateTime object as the first two parameters in this function and the third parameter to get only positive intervals of dates, It is optional.
PHP time() Function
time() function is used to print the current timestamp. It will return the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
The output of the time() function depends upon the default time zone. You can check the default time zone in your php.ini file.
So, if you want to change the time zone then you can change it in the php.ini file or you can also set the time zone programmatically using the
See the following example code to print the current timestamp in seconds.
PHP Get and Set Timezone Programmatically
As I said, we can set the timezone programmatically and print a time accordingly. You need to use the
date_default_timezone_set() function and pass the timezone as a parameter to set the timezone.
If you know the timezone then you can pass it as a string in the time function else you need to know to timezone.
Get Timezone Programmatically
To get the timezone list programmatically in PHP, you have to use the
listIdentifiers() static method of the DateandTime Zone built-in class. Then you can loop through an array and print the timezone list.
See the following example code to print the list of all timezone.
Using the above code, it will print the all timezone list and now you can use them to set the timezone programmatically using PHP code. Let’s see how you can do that.
Set Timezone Programmatically
Now, we will use the
date_default_timezone_set() function to set the timezone using the PHP script code. When you are done with setting the timezone then now you can use all date functions in your code script. It will show you time according to the timezone that you set up.
Look at the following example of setting the timezone programmatically in PHP.
Let’s see some most popular time functions and how we can use them to print a time, make a time, convert a time, micro time, etc.
PHP mktime() Function
mktime() function returns the timestamp in Unix format for a date. It will return false if any error arise.
- hour: Optional. the number of hour
- minute: Optional. the number of minute
- second: Optional. the number of second
- month: Optional. the number of month
- day: Optional. the number of day
- year: Optional. the number of year
Let’s take an example to see what was the day on October 3, 1975. To see this we will use mktime() function and specify the date and then use the date() function with the “l” parameter.
The mktime() function is also useful for doing date arithmetic and validation. It will automatically calculate the correct value for out-of-range input. Let’s have examples.
PHP strtotime() Function
strtotime() function uses to parses an English textual DateTime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
So it will convert the string DateTime to a human-readable timestamp.
Some examples of strtotime() function.
1638094426 1638112426 1638699226 1638983631 1638144000 1637452800 2021-11-27
PHP microtime() Function
microtime() function is also very useful. It will print the current Unix timestamp in microseconds. I normally use this function to know the execution time of the PHP script.
It has only one parameter, if you set that parameter as
true then it will return the timestamp in float number else in string. Default is
There are many more useful PHP date and time functions that you can use for different purposes. To see all the list PHP date and time functions click here.
PHP Date and Time Function Parameters Values
The following table shows you the common parameters and their values that you can use while working with date-time functions in PHP.
|The day of the month (from 01 to 31)|
|A textual representation of a day (three letters)|
|The day of the month without leading zeros (1 to 31)|
|The day name of week|
|A numeric representation of the day (0 for Sunday, 6 for Saturday)|
|The day of the year (from 0 through 365)|
|A full textual representation of a month (January through December)|
|A numeric representation of a month (from 01 to 12)|
|A short textual representation of a month (three letters)|
|A numeric representation of a month, without leading zeros (1 to 12)|
|The number of days in the given month|
|A two digit representation of a year|
|A four digit representation of a year|
|Whether it’s a leap year (1 if it is a leap year, 0 otherwise)|
|The ISO-8601 year number|
|Lowercase am or pm|
|Uppercase AM or PM|
|12-hour format of an hour (1 to 12)|
|24-hour format of an hour (0 to 23)|
|12-hour format of an hour (01 to 12|
|24-hour format of an hour (00 to 23)|
|Minutes with leading zeros (00 to 59)|
|Seconds, with leading zeros (00 to 59)|
|The timezone identifier (Examples: UTC, GMT, Asia/Kolkata)|
|Return formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)|