PHP- DATE/TIME FUNCTIONS

When working in any programming language, dealing with dates and time is a simple task. That is, until time zones have to be supported. PHP’s time () function gives you all the information that you need about the current date and time. It requires no arguments but returns an numeric value.

If you will call to time() function which will return a set of numeric values which is difficult to understand. So to make it easy we can have time format along with the date() function.

<?php

$today = date(“d/m/Y”);

echo “Today is “.$today;

?>

We can use different time format with separator like d-m-Y, d.m.Y and so on.

Display Current Date and Time:  We can use the following characters to format the time string:

<?php

echo date(“h:i:s”) . “<br>”;

echo date(“F d, Y h:i:s A”) . “<br>”;

echo date(“h:i a”);

?>

Here h-hour in 12-hour format, H – hour in in 24-hour format, i – minutes, s – seconds, a -am, pm(Lowercase) and  A – Represent AM, PM (Uppercase)

So it is easy to get the current year by putting the following line- <?php echo date(“Y”);?>?>

The PHP time(): The time() function is used to get the current time as a Unix timestamp

Example: 

<?php

$cDate = 1394003958;

echo(date(“F d, Y h:i:s”, $cDate));

?>

Example: To print date like 10 July 2015

<?php

$date1 = date_create(―2016-05-10”);

echo date_format($date1, ‘j-F-Y’);

?>

 Output: 10 May 2016

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/03/29/php-datetime-functions
YouTube41
YouTube