Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Hide Buttons

how to use date function in php

In this post I will share my expe­ri­ence about how to use the date func­tion present in php library. I will also show how we can con­vert the date in 12 hours for­mat to 24 hours for­mat and vice verse.

Print today’s date — To print today’s date, we need to call the date func­tion by pass­ing at least one para­me­ter men­tion­ing the date for­mat. It returns the string rep­re­sen­ta­tion for today’s date using the pro­vided for­mat. The

$date1 = date('y-m-d'); // y - 2 digit year, m - month
$date2 = date('Y-m-d'); // Y - 4 digit year
$date3 = date('d/m/y'); // d - day with leading zeros
$date4 = date('j/m/Y'); // j - day without leading zeros

echo "Today's date - date1: " . $date1;
echo "\nToday's date - date2: " . $date2;
echo "\nToday's date - date3: " . $date3;
echo "\nToday's date - date4: " . $date4;

Output: 
Today's date - date1: 13-06-20
Today's date - date2: 2013-06-20
Today's date - date3: 20/06/13
Today's date - date4: 20/06/2013

 Print today’s date and time in 24 hrs for­mat — To print today’s date together with time we need to pro­vide addi­tional detail related to the time for­mat apart from date format.

date_default_timezone_set('UTC');
$date1 = date('y-m-d H:i:s'); // H - 24 hr format - values 00 to 23 

date_default_timezone_set('Asia/Calcutta');
$date2 = date('d/m/y H:i:s'); // i - minutes - values 00 to 59
$date3 = date('d/m/Y H:i:s'); // s - seconds - values 00 to 59

echo "Today's date and time - date1: " . $date1;
echo "\nToday's date and time - date2: " . $date2;
echo "\nToday's date and time - date3: " . $date3;

Output:
Today's date and time - date1: 13-06-20 07:13:00
Today's date and time - date2: 20/06/13 12:43:00
Today's date and time - date3: 20/06/2013 12:43:00

Print today’s date and time in 12 hrs for­mat - To print today’s date together with time in 12 hours for­mat, we need to pro­vide addi­tional detail related to the time for­mat apart from date format.

$date1 = date('y-m-d h:i:s'); // h - 12 hr format - values 01 to 12 
$date2 = date('y-m-d h:i:s A'); // A - To print AM/PM
$date3 = date('y-m-d h:i:s a'); // a - To print am/pm

echo "Today's date and time - date1: " . $date1;
echo "\nToday's date and time - date2: " . $date2;
echo "\nToday's date and time - date3: " . $date3;

Output:
Today's date and time - date1: 13-06-20 07:22:45
Today's date and time - date2: 13-06-20 07:22:45 AM
Today's date and time - date3: 13-06-20 07:22:45 am

Con­vert 24 hour to 12 hour : To con­vert the date in 24 hours for­mat to 12 hour for­mat, we need to use the str­to­time func­tion of php. The func­tion str­to­time takes date for­mat­ted in eng­lish as input and return the equiv­a­lent num­ber of sec­onds since Jan­u­ary 1 1970 00:00:00 UTC.

$date1 = date('y-m-d H:i:s'); 
$date2 = date('d/m/y h:i:s A',strtotime($date1)); 

echo "Today's date and time - date1: " . $date1;
echo "\nToday's date and time - date2: " . $date2;

Output:
Today's date and time - date1: 13-06-20 07:28:46
Today's date and time - date2: 20/06/13 07:28:46 AM

Con­vert 12 hour to 24 hour : Sim­i­lar to the pre­vi­ous exam­ple, we can also con­vert the date in 12 hour for­mat to 24 hour for­mat as men­tioned below:

$date1 = date('y-m-d h:i:s A'); 
$date2 = date('d/m/y H:i:s',strtotime($date1)); 

echo "Today's date and time - date1: " . $date1;
echo "\nToday's date and time - date2: " . $date2;

Output:
Today's date and time - date1: 13-06-20 07:32:36 AM
Today's date and time - date2: 20/06/13 07:32:36

 

Leave a Reply

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