In This Article
In this article, we talk about PHP Date functionality and all the operations which we used on web applications.
Generally, we use the PHP date() function for showing dates in a different format.
Some important points about Date PHP function,
- Computer system store the date-time on UNIX timestamp format.
- This measure the time on the second basis since beginning of epoch (midnight Greenwich Mean Time on January 1, 1970, i.e. January 1, 1970 00:00:00 GMT).
- PHP Date function converts the timestamp time format to a human-readable format.
PHP Date Syntax
PHP Date() function Syntax
<?php $CurrentDate = date("d/m/Y"); echo $CurrentDate; ?>
NOTE:PHP date() returns date and time according to the builtin server or where you run that script.
PHP Date function
How to Change PHP Date Format
Changing date format in PHP, PHP date function allowed some characters for changing the format like AM, PM and the day of the week. Some of the characters which is mainly used on the time of development are as follows,
- Y – As a character suggest, refer to Year in four-digit like 2004 or 2020.
- y – Small y also for a year but with last two-digit like 08 or 20.
- M – Represent month as an acronym for Jan to Dec.
- m – Represents numeric of the months like 01 to 12.
- D – Represent the current day of the week, Mon to Sun.
- d – Represents the day of the month with a numeric value like 01 to 31.
PHP Date format representation:
<?php echo date("d-m-Y") . "<br>"; echo date("d/m/Y") . "<br>"; echo date("d.m.Y"); ?>
PHP time() Function
Same as the PHP date function, PHP time function is used to get the current time as UNIX timestamp value.
<?php // Executed at February 11, 2020 07:27:16 $CurrentTimestamp = time(); echo($CurrentTimestamp); ?>
Output: 1581445636Unix timestamp value
We can convert that UNIX timestamp value to human-readable value using PHP Date function. Check below,
<?php $CurrentTimestamp = 1581445636; echo(date("F d, Y h:i:s", $CurrentTimestamp)); ?>
Output: February 11, 2020 07:27:16
Change PHP time Format Representation
As PHP date, similarly PHP time() function has some character for formatting the time.
- s – seconds with leading zeros, like 00 to 59
- a – lowercase anti meridian and post meridian, like am or pm
- h – an hour in 12-hour format with leading zeros, like 01 to 12.
- H – an hour in 24-hour format with leading zeros, like 00 to 23
- i – minutes with leading zeros, like 00 to 59
- A – uppercase Anti meridian and Post meridian, like AM or PM
<?php echo date("F d, Y h:i:s A") . "<br>"; echo date("h:i:s") . "<br>"; echo date("h:i a"); ?>
Output:February 11, 2020 07:37:36 PM
If you want you can use out PHP ONLINE EDITOR for checking those example source code execution.
Want to know more check PHP official website https://www.php.net/manual/en/function.date.php