What is Date function in PHP ?

The PHP Date function is the PHP built-in function which works with the date data types .
The Date function is use to optimize the dates in proper and human readable formats.
The dates can be used for many purpose as the articles published date , articles last updated in database .
Syntax :

<?php date('format', [timestamp]); ?>

Here ,
date()  : is a function that returns the current time on on server .
format : indicates which format of date to want to display , as for example  “Y-m-d” , “F j Y” etc
timestamps : is a optional , if on timestamps is provided it takes the server default time stamps.

Example :

<?php echo date('Y'); ?>

Output

2018 

For the month and year the following characters can be used to format the day,month,year :

  •     d – Day  Representation of the month; two digits with leading zeros (01 or 31)
  •     D – Day  Representation day of the week in text as an abbreviation (Mon to Sun)
  •     m – Month  Representation in numbers with leading zeros (01 or 12)
  •     M – Month  Representation month in text, abbreviated (Jan to Dec)
  •     y – Year Representation in two digits (08 or 14)
  •     Y – Year Representation year in four digits (2008 or 2014)

For Example : 

<?php
echo date("d/m/Y") . "<br>";
echo date("d-m-Y") . "<br>";
echo date("d.m.Y");
?>

Output :

14/07/2018
14-07-2018
14.07.2018

In the same way you can use the following characters to format the time string:

  •     h – Hour Representation in 12-hour format with leading zeros  (01 to 12)
  •     H – Hour Representation  in 24-hour format with leading zeros (00 to 23)
  •     i – Minutes  Representation with leading zeros (00 to 59)
  •     s – Seconds  Representation with leading zeros (00 to 59)
  •     a – Lowercase Representation for ante meridiem and post meridiem (am or pm)
  •     A – Uppercase  Representation for Ante meridiem and Post meridiem (AM or PM)

For Example :

<?php
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>

Output  :

04:17:14
July 14, 2018 04:17:14 AM
04:17 am

Leave a Reply

Your email address will not be published.Required fields are marked *