Discover all about the PHP Date and Time function with examples. Learn how to use them effectively in your applications to get the current date and time in PHP:
In this tutorial, you will learn what the PHP date function is, PHP date, PHP time, and frequently asked questions (FAQs) related to this topic.
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Read Through the Series of PHP Tutorials
Table of Contents:
PHP Date and Time Functions

PHP Date Function
The date() function formats a date and/or a time.
Syntax:
date(format,timestamp);
The following table shows details about the parameters that we can pass to the date() function:
| Parameter | Data Type | Compulsory/Optional | Description | |
|---|---|---|---|---|
| 1 | Format | String | Compulsory | The format of the PHP timestamp. |
| 2 | Timestamp | Integer | Optional | PHP timestamp. Default value is current time. |
Furthermore, the default timezone defined in the php.ini file is used by the PHP date functions.
Below shows an example of the date() function. You can practice this example by running the following programming code:
<?php
echo date("d/m/Y H:i:s");
?>
The below text shows the browser output (sample output) of the above programming code:
11/02/2022 13:01:01
The date() function formats a date. The format characters shown in the following table can be used to format a date.
| Format Character | Category | Description | Representation Format | Returned Values/ Examples | |
|---|---|---|---|---|---|
| 1 | d | Day | Day of the month in two digits (with a leading zero). | Numeric | Returned Values: 01 to 31 |
| 2 | D | Day | Day of the week as an abbreviation in three characters. | Textual | Returned Values: Mon through Sun |
| 3 | j | Day | Day of the month without a leading zero. | Numeric | Returned Values: 1 to 31 |
| 4 | l (lowercase L) | Day | Day of the week in the full form. | Textual | Returned Values: Sunday through Saturday |
| 5 | N | Day | Day of the week in ISO 8601 representation. | Numeric | Returned Values: 1 (for Monday) through 7 (for Sunday) |
| 6 | S | Day | Suffix for the day in two characters. | Textual | Returned Values: st, nd, rd or th |
| 7 | w | Day | Day of the week without a leading zero. | Numeric | Returned Values: 0 (for Sunday) through 6 (for Saturday) |
| 8 | z | Day | Day of the year without leading zeros. | Numeric | Returned Values: 0 through 365 |
| 9 | W | Week | Week number of the year in ISO 8601 representation. | Numeric | Example: 06 (weeks starting on Monday) |
| 10 | F | Month | Full name of the month. | Textual | Returned Values: January through December |
| 11 | m | Month | The number of the month in two digits (with a leading zero). | Numeric | Returned Values: 01 through 12 |
| 12 | M | Month | Name of the month as an abbreviation in three characters. | Textual | Returned Values: Jan through Dec |
| 13 | n | Month | The number of the month without a leading zero. | Numeric | Returned Values: 1 through 12 |
| 14 | t | Month | The number of days in the month. | Numeric | Returned Values: 28 through 31 |
| 15 | L | Year | Indicates whether the year is a leap year or not. | – | Returned Values: 1 if a leap year, otherwise 0 |
| 16 | o | Year | Week-numbering year in ISO 8601 representation. | Numeric | Example: 2022 |
| 17 | y | Year | Year in two digits. | Numeric | Example: 22 |
| 18 | Y | Year | Year in four digits. | Numeric | Example: 2022 |
Here is an example of how to format a date using the date() function. You can practice this example by running the following programming code:
<?php
echo date("d-m-y")."<br>";
echo date("d-m-Y")."<br>";
echo date("Y/m/d")."<br>";
echo date("Y.m.d");
?>
The below text shows the browser output (sample output) of the above programming code:
11-02-22 11-02-2022 2022/02/11 2022.02.11
PHP Time
The date() function formats a time. The format characters shown in the following table can be used to format a time.
| Format Character | Category | Description | Example/Returned Values | |
|---|---|---|---|---|
| 1 | a | Time | am or pm* in lowercase | Returned Values: am or pm |
| 2 | A | Time | AM or PM* in uppercase | Returned Values: AM or PM |
| 3 | B | Time | Swatch Internet time. | Returned Values: 000 through 999 |
| 4 | g | Time | 12-hour format of an hour without a leading zero. | Returned Values: 1 through 12 |
| 5 | G | Time | 24-hour format of an hour without a leading zero. | Returned Values: 0 through 23 |
| 6 | h | Time | 12-hour format of an hour (with a leading zero). | Returned Values: 01 through 12 |
| 7 | H | Time | 24-hour format of an hour (with a leading zero). | Returned Values: 00 through 23 |
| 8 | i | Time | Minutes (with a leading zero) | Returned Values: 00 to 59 |
| 9 | s | Time | Seconds (with a leading zero) | Returned Values: 00 through 59 |
| 10 | e | Timezone | Timezone identifier | Example: Asia/Kolkata |
| 11 | I(Uppercase i) | Timezone | Indicates whether the date is in daylight saving time or not. | Returned Values: 1 if daylight saving time, otherwise 0 |
| 12 | O | Timezone | Difference to GMT* without colon between hours and minutes. | Example: +0530 |
| 13 | P | Timezone | Difference to GMT* with a colon between hours and minutes. | Example: +05:30 |
| 14 | p (PHP 8.0.0 onwards) | Timezone | The same as P, but returns Z instead of +00:00 | Example: +05:30 |
| 15 | T | Timezone | Timezone abbreviation (if known)/the GMT* offset. | Example: IST |
| 16 | Z | Timezone | Timezone offset in seconds. | Returned Values: -43200 through 50400 |
*Note:
- GMT stands for Greenwich time.
- AM or am stands for ante meridiem.
- PM or pm stands for post meridiem.
Below shows an example of how to format time using the date() function. You can practice this example by running the following programming code:
<?php
echo date("H:i:s")."<br>";
echo date("h:i:s A");
?>
The below text shows the browser output (sample output) of the above programming code:
13:01:54 01:01:54 PM
Full PHP Date/Time
The date() function formats a +date and time. The format characters shown in the following table can format the date and time.
| Format Character | Category | Description | Example | |
|---|---|---|---|---|
| 1 | c | Full Date/Time | ISO 8601 date | 2022-02-11T07:43:41+05:30 |
| 2 | r | Full Date/Time | RFC 2822 formatted date | Fri, 11 Feb 2022 07:43:41 +0530 |
| 3 | U | Full Date/Time | Seconds since the Unix Epoch* | 1644545621 |
*Note: The Unix Epoch is January 1, 1970, 00:00:00 GMT.
Below shows an example of how to format date and time using the date() function. You can practice this example by running the following programming code:
<?php
echo date("c")."<br>";
echo date("r")."<br>";
echo date("U");
?>
The below text shows the browser output (sample output) of the above programming code:
2022-02-11T07:43:41+05:30 Fri, 11 Feb 2022 07:43:41 +0530 1644545621
Common Functions Associated with Date & Time
PHP has several built-in functions to work with date and time.
Some of the important date/time functions are shown in the list below:
- date_default_timezone_get() – Gets the default timezone.
- date_default_timezone_set() – Sets the default timezone.
- date_parse() – Returns an associative array with detailed information about a given date/time.
- getdate() – Gets date/time information.
- localtime() – Gets the local time.
- microtime() – Returns the current Unix timestamp with microseconds.
- mktime() – Returns Unix timestamp for a date.
- time() – Returns the current Unix timestamp.
mktime Function
The mktime() function gets the Unix timestamp for a date.
Syntax:
mktime(hour, minute, second, month, day, year);
The following table shows details about the parameters that we can pass to the mktime() function.
| Parameter | Data Type | Compulsory/Optional | Description | |
|---|---|---|---|---|
| 1 | Hour | Integer | Compulsory in PHP 8 | The number of the hour. It is calculated relative to the beginning of the day determined by month, day and year. |
| 2 | Minute | Integer | Optional | The number of the minute. It is calculated relative to the beginning of the hour. |
| 3 | Second | Integer | Optional | The number of seconds. It is calculated relative to the beginning of the minute. |
| 4 | Month | Integer | Optional | The number of the month. It is calculated relative to the end of the previous year. |
| 5 | Day | Integer | Optional | The number of the day. It is calculated relative to the end of the previous month. |
| 6 | Year | Integer | Optional | The number of the year. |
Below shows a few examples of the mktime() function. You can practice these examples by running the following programming code:
Example 1:
<?php
$timestamp = mktime(0, 0, 0, 1, 15, 2022);
echo date("M-d-Y", $timestamp);
?>
The below text shows the browser output (sample output) of the above programming code:
Jan-15-2022
Example 2:
<?php
echo date("d/m/Y h:i:s A", mktime(14, 30, 0, 1, 1, 22))."<br>";
echo date("d/m/Y h:i:s A", mktime(14, 30, 0, 1, 1, 2022))."<br>";
echo date("d/m/Y h:i:s A", mktime(14, 30, 0, 13, 1, 2021))."<br>";
echo date("d/m/Y h:i:s A", mktime(14, 30, 0, 12, 32, 2021));
?>
The below text shows the browser output of the above programming code (sample output):
01/01/2022 02:30:00 PM 01/01/2022 02:30:00 PM 01/01/2022 02:30:00 PM 01/01/2022 02:30:00 PM
Frequently Asked Questions
1. What is the PHP date function?
It is the function used to format a date and/or a time.
2. How can I get the current date in the yy-mm-dd format using the PHP date format?
You can use the following code to get the current date in the yy-mm-dd format:
<?php
echo date(‘y-m-d’);
?>
3. How can I assign the current date to a PHP variable and get the current time?
You can use the following code for it.
<?php
$currentDate = date(‘d/m/Y’);
echo $currentDate;
?>
4. How can I get the current Indian time in PHP?
You can use the following code to get the current Indian time:
<?php
date_default_timezone_set(‘Asia/Kolkata’);
echo date(‘d/m/Y H:i’);
?>
5. Which PHP function do you use to format a date?
There are several in-built functions available in PHP to format a date, like date(), date_format(), etc.
6. What is the PHP time function?
It is a function that returns the current Unix timestamp
Conclusion
The PHP date() function formats the date and time. Further, there are several other built-in functions available in PHP to format the date and time.
The default timezone defined in the php.ini file is used by the date functions.






