PHP Date and Time
PHP makes it easy to work with date and time using built-in PHP date()
, time()
, mktime()
, and strtotime()
functions.
When you need a particular date and time format, PHP date()
is the most important function. PHP date() supports ‘format’ parameter that provides a wide range of formatting options according to your requirement.
PHP date() function:
Syntax: date(format, timestamp)
Parameter | Description |
---|---|
format | Required. This specifies the format of date and time that you want to return. |
timestamp | Optional. Specify UNIX timestamp required date/time. By default, the current date and time are taken. |
Example: Display current date
<?php
$today = date("d-m-Y");
echo $today;
?>
We did not provide any timestamp in the second parameter. Therefore, it will take the current timestamp (current date and time) and display it according to the format "d-m-Y"
. For example: 20-06-2020
.
Note: You can pass time format characters as well if need to display time in the same function. See more details about the format parameters in next table.
Common date and time format characters:
Character | Description (Format) |
---|---|
d | Day of the month with 2 digits (leading zeros) format (01 to 31) |
D | Day name of the week in three letters (Mon to Sun) |
m | Month in numbers with 2 digits (01 or 12) |
M | Month in three letters (Jan to Dec) |
F | Month full textual representation (January to December) |
y | Year in 2 digits (03 or 21) |
Y | Year in 4 digits (1995 or 2020) |
h | Hour in 12-hour format with 2 digits (01 to 12) |
H | Hour in 24-hour format with 2 digits (01 to 24) |
i | Minutes in 2 digits (00 to 59) |
s | Seconds in 2 digits (00 to 59) |
g | Hour in 12-hour format without leading zeros (1 to 12) |
G | Hour in 24-hour format without leading zeros (1 to 24) |
a | Ante meridiem and Post meridiem in Lowercase (am or pm) |
A | Ante meridiem and Post meridiem in Uppercase (AM or PM) |
You can use a combination of above characters in the ‘format’ parameter to get desired date and time formatting.
Mostly used date formats in PHP:
PHP date function with useful format parameters | Output |
---|---|
<?php echo date("Y-m-d"); // MySQL date format ?> | 2020-06-20 |
<?php echo date("F j, Y, g:i A"); ?> | June 20, 2020, 3:22 PM |
<?php echo date("l, F jS, Y"); ?> | Saturday, June 20th, 2020 |
<?php echo date("g:i:s a"); // Time only ?> | 3:22:24 pm |
<?php echo date("d/m/Y"); ?> | 20/06/2020 |
<?php echo date("Y/m/d g:i a"); ?> | 2020/06/20 3:22 pm |
Try and Run code: https://paiza.io/projects/wEMzCJDV6a62NY3smz6tTA
PHP time() Function:
The time()
function returns the current time as a Unix timestamp, and we can pass it to date()
function to change its format according to requirement.
What is PHP Timestamp?
The Timestamp is the number of seconds calculated between the current time and 1st January 1970 00:00:00 (GMT). This is also called Unix Timestamp.
Example: Using PHP time() Function
First, we will display current timestamp and date. Therefore, calculate next month timestamp and display date.
<?php
// Get current timestamp
$current = time();
echo "Current Timestamp: " .$current;
echo "<br/><br/>";
echo 'Current Date: '.date("Y-m-d", $current);
echo "<br/><br/>";
// One month calculation using 30 days; 24 hours; 60 mins; 60 secs
$next_month = time() + (30 * 24 * 60 * 60);
echo "Next Month Timestamp: " .$current;
echo "<br/><br/>";
echo 'Next Month: '. date('Y-m-d', $next_month) ."\n";
?>
Try and Run Code: https://paiza.io/projects/gat_YF2oFLtnVmn3-SARwA
PHP mktime() Function:
The mktime()
function returns Unix timestamp for given hour, minute, second, month, day and year.
Syntax: mktime( $hour, $minute, $second, $month, $day, $year, $is_dst)
In the following example, we will first make a timestamp by specifying the month, day, and year into mktime()
. Then, use date function to display it with the format: Thursday, June 18th, 2020.
<?php
// Using mktime() & date() show required format
echo date("l, F jS, Y", mktime(0, 0, 0, 6, 18, 2020));
?>
Try and Run Code: https://paiza.io/projects/UEyYOmqm9XnYvVaUqNfTNg
PHP strtotime() Function:
The strtotime()
converts English textual date-time description to a UNIX timestamp which can be displayed in different formats using date()
function.
Here is the example of commonly used English textual formats:
<?php
$current_timestamp = strtotime("now");
$past_date = strtotime("10 September 2000");
$tomorrow = strtotime("+1 day");
$future_timestamp = strtotime("+1 week 2 days 4 hours 2 seconds");
echo date("d/m/Y", $current_timestamp);
echo "<br/><br/>";
echo date("d/m/Y", $past_date);
echo "<br/><br/>";
echo date("d/m/Y", $tomorrow);
echo "<br/><br/>";
echo date("d/m/Y", $future_timestamp);
echo "<br/><br/>";
?>
Try and Run Code: https://paiza.io/projects/kEIgZVL2QsGxSnBW-4oxfw
Tips:
- All date and time functions use the default time zone (Mostly UTC) set in the php.ini file.
- If you want to display your local time zone, you can set it using
date_default_timezone_set('Asia/Kolkata');
function.
These are the wide range of useful PHP functions to work with date and time. You can find more details about the parameters and options in official documentation and reference websites given below:
Important Links:
- PHP date() function: https://www.php.net/manual/en/function.date.php
- PHP time() Function: https://www.php.net/manual/en/function.time.php
- PHP mktime() Function: https://www.php.net/manual/en/function.mktime.php
- PHP strtotime() Function: https://www.php.net/manual/en/function.strtotime.php
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |