Working with Date and Time Functions in PHP5 and PHP7
Working with date and time functions is very common in PHP. Date and time functions help you to get the current date and time from the server and also enables you to format the date and time in a proper manner etc.
PHP is also quite clever while working with date and time functions which we will see through this tutorial and will also learn about how to use these date and time functions in PHP.
Get a Simple Date with PHP date()
Function
PHP date()
function formats the given timestamp to a more readable date and time; and it formats the timestamp as per our instructions. The PHP date()
function takes two parameters:
- format (specifies the format of the timestamp)
- timestamp (sequence of characters representing the date and/or time at which a certain event has occurred)
Here format is required and timestamp is an optional parameter. If you omit the timestamp parameter, then the current date and time will be used by the function.
There are also some characters (d, m, Y, l) which are commonly used in the format parameter to specify the format of the timestamp or date.
- d: Indicates the day of the month (from 01 to 31).
- m: Indicates the month (01 to 12).
- Y: Indicates the year (in four digits).
- l (lowercase ‘L’): Indicates the day of the week.
Characters like “/”, “.” and “-” can also be used for further formatting.
Syntax
date(format, timestamp);
Example Usage
The Example below shows you how to use date() function:
<?php
echo “Today, date is: “,date(“d-m-Y”),”<br/>”;
echo “Today, date is: “,date(“d/m/Y”),”<br/>”;
echo “Today, date is: “,date(“d.m.Y”),”<br/>”;
echo “Today, day is: “,date(“l”);
?>
In the example above, the format parameter (“d-m-Y”) is specified, but as we did not specify the timestamp parameter, unction uses the current date and time and gives the output which is shown below:
Today, date is: 16-07-2020
Today, date is: 16/07/2020
Today, date is: 16.07.2020
Today, day is: Thursday
Get a Simple Time with PHP date()
Function
There are also some characters (h, i, s, a) which are used to format the timestamp to a more readable time.
- h: Indicates the 12 hour format of an hour with leading zeros (01 to 12).
- i: Indicates the Minutes with leading zeros (00 to 59).
- s: Indicates the Seconds with leading zeros (00 to 59).
- a: Indicates the am (ante meridiem) and pm (post meridiem).
Example:
<?php echo “Time is: “,date(“h:i:sa”);?>
The Output of the example above, should be look like this:
Time is: 06:39:13pm
One thing you should have to remember always is that the date()
function returns the current date and time of the server unless specified otherwise.
PHP time()
Function
The PHP time() function returns the current time as a Unix timestamp (the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified). You can also format it into the required format of date and time by using the date() function.
Example
<?php
$time=time();
echo $time,”<br>”;
echo date(“Y-m-d”,$time);
?>
The Output of the example above, should be look like this:
1594908926
2020-07-16
Here 1594908926
is the number of seconds passed since January 1 1970 00:00:00 GMT at the time of writing this word. See EpochConverter for more info.
Time Zone (date_default_timezone_set
), mktime
and strtotime
Functions
Get Time Zone, mktime() and strtotime() Functions are helpful in getting time zones, returning the Unix timestamp for a date and converting a readable string to a date and time.
Get/Set Your Time Zone in PHP
Sometimes, the time which you got back from the server is incorrect because the server from which you got time, might be placed is in another country or set up for a different time zone. But, if you want the correct time according to a specific location then, you have to set the correct time zone.
Example
<?php
date_default_timezone_set(“Asia/Calcutta”);
echo “Time is: “, date(“h:i:s a”);
?>
The output of the example above, should be look like this:
Time is: 06:41:49 pm
Without setting the correct time zone, the server returns 01:11:49 pm to me which is GMT time. But, when I set the correct time zone then the server returns 06:41:49 pm to me, which is the correct time according to my time zone.
You can also pick your time zone from the list of time zones which is supported by PHP 5.
PHP mktime()
Function
The PHP mktime() function returns the Unix timestamp for a date and time.
What is Unix Timestamp?
The number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax of mktime()
function
mktime(hour, minute, second, month, day, year);
For example:
<?php
$datetime=mktime(13,15,00,12,29,2015);
echo $datetime, “<br/>”;
echo date(“Y/m/d h:i:s a”,$datetime);
?>
The output of the example above, should be look like this:
1451391300
2015/12/29 01:15:00 pm
In the example above, mktime() function returns the Unix timestamp which is further formatted using the date() function.
PHP strtotime() Function
The PHP strtotime() function converts a readable String to a Unix timestamp.
Syntax of strtotime() function
strtotime(time, now)
Example:
<?php
$datetime=strtotime(“December 29 2015 9:00am”);
echo $datetime,”<br/>”;
echo date(“Y/m/d h:i:s a”,$datetime);
?>
The output of the example above, should be look like this:
1451376000
2015/12/29 09:00:00 am
Other examples of strtotime():
Example 1
<?php
$datetime=strtotime(“yesterday”);
echo $datetime,”<br/>”;
echo date(“Y/m/d h:i:s a”,$datetime);
$datetime=strtotime(“+2 weeks”);
echo “<br/>”;
echo date(“Y/m/d”,$datetime);
?>
Output:
1594908900
2020/07/15 12:00:00 am
2020/07/30
Example 2
<?php
$datetime1=strtotime(“Jan 01 2021″);
$datetime2=ceil(($datetime1-time())/60/60/24);
echo $datetime2,” days left”;
?>
Output:
168 days left
Real life example: https://days.to/until/1-january
As you seen, the php is quite clever while working with date and time functions, but always check the values that you’d passed into the strtotime() function because strtotime() function is not so perfect.