In this PHP tutorial we will see how to format the date and time.
The PHP Date() Function
The PHP Date() function is used to formats a timestamp to a more readable date and time.
The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ). For Example….See below:
<?php print time(); ?>
Since this is not readable(1572854758) format for humans being to read, For humans convenient , PHP converts a timestamp to a format that is readable to humans and dates from your notation into a timestamp the computer understands. The syntax for the PHP date() function can be given below.
There are two important facts generated which given below:
1. The format parameter in the date() function is required which is tell us to specifies the format of returned date and time.
2.Second think we can say timestamp is an optional parameter, if not included then current date and time will be used as a output. The following example clearer your all query which is displays today’s date:
<?php $today = date("d/m/Y"); echo $today; ?>
Types of Parameters the Dates with PHP
There are some the date-related formatting characters that are commonly used in format string which given below and also see a example for batter understanding :
- d – Represent day of the month; (01 or 31)
- D – Represent day of the week in text as an abbreviation (Mon to Sun)
- m – Represent month in numbers with leading zeros (01 or 12)
- M – Represent month in text, abbreviated (Jan to Dec)
- y – Represent year in two digits (08 or 14)
- Y – Represent year in four digits (2008 or 2014)
The section of the date can be separated by putting other characters, like hyphens (-), dots (.), slashes (/), or spaces to add additional visual formatting.
<?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"); ?>
Types of Parameters the Times with PHP
Similarly as a Date Formatting you can use the following characters to format the time string see below:
- h – Represent hour in 12-hour format with leading zeros (01 to 12)
- H – Represent hour in in 24-hour format with leading zeros (00 to 23)
- i – Represent minutes with leading zeros (00 to 59)
- s – Represent seconds with leading zeros (00 to 59)
- a – Represent lowercase ante meridiem and post meridiem (am or pm)
- A – Represent uppercase Ante meridiem and Post meridiem (AM or PM)
<?php echo date("h:i:s") . "<br>"; echo date("F d, Y h:i:s A") . "<br>"; echo date("h:i a"); ?>
The PHP time() Function
The PHP time() function is used to get the current time as a Unix timestamp. For example:
<?php print time(); ?>
For humans convenient this output ,PHP converts a timestamp to a format that is readable to humans introduce date() function.
<?php $timestamp = 1572854758; echo(date("F d, Y h:i:s", $timestamp)); ?>
PHP Date & Time Function with More Example
|Functions||What it Does|
|checkdate()||Checks a date for Validity|
|strtotime()||Creates Timestamps from English-language Descriptions|
|gmdate()||Expresses a timestamp in GMT|
|date_diff()||It will Returns Difference between two dates|
|date( )||Formats a local date and time|
|getdate()||Returns date/time information of a timestamp or the current local date/time|
|mktime()||Returns the Unix timestamp for a date|
|time()||Returns the current time as a Unix timestamp|