Date and time are some of the most frequently used operation in PHP while executing SQL queries or designing a site. PHP serves with the in-built date and time function for these task. We will use PHP date and time function to format a date or time into a human readable format. Some of the built-in functions in PHP for date and time are discussed below.
Date function Syntax & Example
Here, date() function returns the current time from the server. So this formats a timestamp to a more readable date and time.
|Format||Required. Specifies the format of the timestamp. That is |
“Y-m-d” for PHP date format YYYY-MM-DD
“Y” to display the current year
|Timestamp||Optional. Specifies a timestamp. If it is not included then php current date and time on the server is used.|
Let’s look at the basic example that displays the date:
<?php echo "Today's date is :"; $today = date("d/m/Y"); echo $today; ?> //Output Today's date is :26/02/2019
Formatting Parameter of the Date Function
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
- d – Represents the day of the month (01 to 31)
- D – Represents day of the week (sun to sat)
- l (lowercase ‘L’) – Represents the day of the week (sunday to satarday)
- m – Represents a month (01 to 12)
- M – Represents month in text (Jan to Dec)
- y – Represents a year (in two digits – 18 or 19)
- Y – Represents a year (in four digits – 2018 or 2019)
Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.
The example below formats today’s date in different ways:
<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?> //Output: Today is 2019/02/26 Today is 2019.02.26 Today is 2019-02-26 Today is Tuesday
Time Function Example
We use the time() function to get the current time as a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT).
Let’s look at the basic example that displays the current Time:
<?php echo time(); ?> //Output 1551164459
Formatting parameters of the Time Function
Here are some characters that are commonly used for times:
- 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)
- A – Uppercase ante meridian and post meridian (AM or PM).
The example below outputs the current time in the specified format:
<?php echo "The time is " . date("h:i:sa"); ?> //Output The time is 02:10:50pm
The Let’s see the example of date function with the Timestamp.
<?php $timestamp = time(); echo($timestamp); echo "\n"; echo(date("F d, Y h:i:s A", $timestamp)); ?> //Output 1551169870 February 26, 2019 02:16:10 PM
Get Your Time Zone
If you didn’t get the correct time from the code, it’s most likely that your server is in another country or set up for a different timezone in the server. Thus if you need a current time according to your current location then you can set a timezone.
The example below sets the timezone to “Asia/Kathmandu”, then outputs the current time in the specified format:
<?php date_default_timezone_set("Asia/Kathmandu"); echo "The time is " . date("h:i:sa"); ?> //Output The time is 02:22:42pm
PHP mktime() for creating Date
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used (as shown in the examples above).
Let’s see the example of the mktime() function below.
<?php $day = mktime(7, 25, 59, 2, 28, 2019); echo "Created date is " . date("Y-m-d h:i:sa", $day); ?> //Output Created date is 2019-02-28 07:25:59am
Create a Date From a String With PHP strtotime()
The PHP strtotime() function is used to convert a human readable string to a Unix time.
The example below creates a date and time from the strtotime() function:
<?php $date=strtotime("02:47pm February 26 2019"); echo date("Y-m-d h:i:sa", $date) . "\n"; $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "\n"; $date=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $date) . "\n"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date); ?> //Output : 2019-02-26 02:47:00pm 2019-02-27 12:00:00am 2019-03-02 12:00:00am 2019-05-26 09:03:08am