In this PHP tutorial, you will learn about the latest time and date functions.
PHP version 8
Before going into this, you must prepare yourself for the latest version of PHP 8 released with plenty of new functions, optimizations, and structural changes. We will discuss all of them step by step in this series of PHP 8 tutorials.
To learn more about the latest PHP release, click here.
Date and Time functions in Php 8
In this section, you will learn date and time functions in PHP’s latest version 8.0.
|Format||Set the format of the timestamp in the returned value. This function accept all formats accepted by DateTimeInterface::format().|
|Timestamp||Set the timestamp in the returned value. By default, it returns the current date and time. In other words, it defaults to the value of the time() function.|
To specify the format, you must know about the following characters representing the corresponding format.
|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|
In Php 8.0 timestamp is now nullable.
|Success||It returns a string formatted as defined by the format parameter.|
|Errors/Exceptions||If the time zone is not valid date function will return E_WARNING.|
To learn about valid time zones you can go through this link.
Get Date in specific format
It is used to get the current data in the specified format.
<?phpa echo " Date is " . date("Y/m/d") . " "; echo " Date is " . date("Y.m.d") . " "; echo " Date is " . date("Y-m-d") . " "; echo " Date is " . date("l"); ?>
How to use date function to get current time in php
To get the current time in the specific format using the date function, pass the time characters in the date function. The most important and useful time characters in PHP are listed below.
|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)|
Get current time in specific format
<?php echo "Curent time is " . date("H:i:s"); ?>
– Does date function return client time or server time?
The above function will print the current time. The important thing to keep in mind here is that the date function returns the current date and time of the server machine. Most people get confused if this function will return client time, but that is not what it does.
– How to set the time zone of date function in php
If you are not getting the correct time using the above example, your server may have been placed in another country and the time zone of your current location and server location is different. In this case you can use date_default_timezone_set(“Asia”); function to set the time zone of date function.
<?php date_default_timezone_set("Asia"); echo "The server time in my zone is " . date("h:i:sa"); ?>
What is UNIX time stamp?
Unix timestamp is the number of seconds between 01 January-1970 and the current time specified.
How to create date in php
You can create your own timestamp or date in PHP using the time function. In this section, you will learn about mktime function in PHP 8.
mktime(hour, minute, second, month, day, year)
The parameters of the function are clear by their names. Let’s understand it with an example.
<?php $date=mktime(7, 11, 34, 6, 09, 2021); echo "My created date is " . date("Y-m-d h:i:s", $date); ?>
How to create date from string in php
Php has a very useful date function called strtotime(). This function converts a human readable date string into a Unix timestamp. If you want to know about Unix time, you can read it from the above section.
<?php $date=strtotime("11:00am August 14 2021"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>
The string accepted by the strtotime function must be correct and valid formatted, otherwise, you will not get the correct in the output. Following are some examples related o strtotime function.
<?php $date=strtotime("yesterday"); echo date("Y-m-d h:i:sa", $date); $d=strtotime("next Friday"); echo date("Y-m-d h:i:sa", $date); $d=strtotime("+4 Months"); echo date("Y-m-d h:i:sa", $date); ?>
How to take difference between two dates in php
Date_diff() method subtracts two valid dates and outputs the difference in form of a number of days.
date_diff(datetime1, datetime2, absolute)
The first and second parameters are date objects to be subtracted and the third parameter is a Boolean. By default, it is false. If it is set to true, the difference will be positive.
<?php $today=date_create("2021-09-05"); $old_date=date_create("2021-08-12"); $difference=date_diff($today,$old_date); ?>
How to add some days, months, years or hours, minutes and seconds to a date in php?
If you are a PHP developer, then you might have gone through the situations where you need to add some amount of time or date in another date object. Php provides the date_add function to add these.
|Date object||Specifies a DateTime object returned by date_create()|
|Interval||Specifies a DateInterval object|
<?php $date_to_be_added=date_create("2021-09-15"); date_add($date_to_be_added,date_interval_create_from_date_string("10 days")); echo date_format($date,"Y-m-d"); ?>
To study more about the time and date functions in the latest PHP 8, refer to the official PHP 8 page.