PHP DATE FORMAT EXAMPLE


Php developer required to format the php date regular at the time of development.
In php scripting DateTime() function returns the object of the DateTime class. This class can be used to format the different date in php script.

How to use the DateTime() function of php?

It is used  to get date object and format the string. See the below example that will return only date and will displayed in the mm/dd/yyyy format.

<?php
$date=new DateTime();
echo $date->format(‘Y-m-d’);
?>

output of the above example will be:

2015-02-23 // yyyy-mm-dd

In format option we can pass the following parameter as per requirement of the date format and it meaning is given below.

Y- four digit Full Year of Date
y- two digit of year of date

m- two digit of Month
F – A full text of a month (January through December)
M – A short text  of a month (three letters)

d- two digit of Day
D – A text of a day (three letters)
l (lowercase ‘L’) – A full textual representation of a day

W –  week number of year (weeks starting on Monday)

h – 12-hour format of an hour (01 to 12)
H – 24-hour format of an hour (00 to 23)
i – Minutes with leading zeros (00 to 59)
s – Seconds, with leading zeros (00 to 59)

a – Lowercase am or pm
A – Uppercase AM or PM

Another example below display the full date and time

<?php
$date=new DateTime();
echo $date->format(‘Y-m-d H:i:s’);

?>

output of the above example will be:

2015-02-23 20-30-10 // yyyy-mm-dd hh:mm:ss

Next example will display the time with am and pm

<?php
$date=new DateTime();
echo $date->format(‘h:i:s a’);

?>

output of the above example will be:

8:30:10 p.m // hh:mm:ss p.m

we can also use the any / instead of the – in date format see the below example

<?php
$date=new DateTime();
echo $date->format(‘y/m/d’);

?>

output of the above example will be:

15/02/23 // yy/mm/dd



Kalpesh Patel

Founder Of Nexus Software Group Leading IT solution and service provider and offering a full range of application development and I.T. consultancy services. Nexus Software Group Contributors In: post free ads online in India and post free ads online in USA