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 experience about how to use the date function present in php library. I will also show how we can convert the date in 12 hours format to 24 hours format and vice verse.

Print today’s date – To print today’s date, we need to call the date function by passing at least one parameter mentioning the date format. It returns the string representation for today’s date using the provided format. 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 format – To print today’s date together with time we need to provide additional detail related to the time format 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 format – To print today’s date together with time in 12 hours format, we need to provide additional detail related to the time format 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

Convert 24 hour to 12 hour : To convert the date in 24 hours format to 12 hour format, we need to use the strtotime function of php. The function strtotime takes date formatted in english as input and return the equivalent number of seconds since January 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

Convert 12 hour to 24 hour : Similar to the previous example, we can also convert the date in 12 hour format to 24 hour format as mentioned 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 *