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:

Example#1

<?php
   print time();
?>

Output:

1572854758

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.

Syntax:


   date(format,timestamp)

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:

Example#2

<?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.

Example#3


<?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)

Example#3


<?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:

Example#

<?php
   print time();
?>

Output:

1572854758

For humans convenient this output ,PHP converts a timestamp to a format that is readable to humans introduce date() function.

Example#

<?php
 $timestamp = 1572854758;
echo(date("F d, Y h:i:s", $timestamp));
?>

Output:

November 04, 2019 08:05:58




PHP Date & Time Function with More Example

PHP Date/Time Common Functions
FunctionsWhat 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

Pin It on Pinterest

Shares
Share This