In this tutorial, you will learn about the latest time and date functions with PHP 8. To learn more about the latest PHP release, click here.
How do I get the current date and time in PHP?
Get the current date given a timezone in PHP?
Convert timestamp to time ago in PHP?
Convert a date format in PHP
How do I use PHP to get the current year?
Correctly determine if the date string is a valid date in that format
Convert timestamp to readable date/time PHP
Converting string to Date and DateTime
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 the 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 to this link.
Get Date in a specific format
It is used to get the current data in the specified format.
<?php 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 the date function to get the 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 the current time in a specific format
<?php echo "Curent time is " . date("H:i:s"); ?>
Does the 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 the 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 the 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 a date from a 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 to the strtotime function.
Example of Strtotime
<?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 get the difference between two dates in php?
The date_diff() method subtracts two valid dates and outputs the difference in the form of a number of days.
<?php date_diff(datetime1, datetime2, absolute) ?>
The first and second parameters are date objects which will be subtracted and the third parameter is a Boolean. By default, it is set to 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 days, months, years or hours, minutes, and seconds to date in PHP1?
If you are a PHP developer, you might have had a situation where you needed to add some time or date in another date object. For this PHP provides us with the date_add function.
|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 read more about the time and date functions in the latest PHP 8 version, please refer to the official PHP 8 page.
You can watch a short video related to the PHP date and time functions.