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
date(format, timestamp);
Example:
1 2 3 4 |
<?php $CurrentDate = date("d/m/Y"); echo $CurrentDate; ?> |
1 |
Output: 11/02/2020 |
NOTE:
PHP date() returns date and time according to the builtin server or where you run that script.
PHP Date functiondate()
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:
1 2 3 4 5 |
<?php echo date("d-m-Y") . "<br>"; echo date("d/m/Y") . "<br>"; echo date("d.m.Y"); ?> |
Output:
11-02-2020
11/02/2020
11.02.2020
PHP time() Function
Same as the PHP date function, PHP time function is used to get the current time as UNIX timestamp value.
1 2 3 4 5 |
<?php // Executed at February 11, 2020 07:27:16 $CurrentTimestamp = time(); echo($CurrentTimestamp); ?> |
Output: 1581445636
Unix timestamp value
We can convert that UNIX timestamp value to human-readable value using PHP Date function. Check below,
1 2 3 4 |
<?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
1 2 3 4 5 |
<?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
07:37:36
07:37 pm
If you want you can use out PHP ONLINE EDITOR for checking those example source code execution.
http://codingtasks.net/how-to-validate-date-string-in-php/
Want to know more check PHP official website https://www.php.net/manual/en/function.date.php
Happy Coding..!
One Reply to “PHP DATE | PHP Date Function”