PHP Date and Time

Current date and time can get using PHP function.
User registration time or want to know the user last login time, booking date or time for order product etc all are possible to get by date() function in PHP.

Date function syntax

Output

Explanation

date() function has many different parameters. All different parameters have different meaning. Like in above example

D is return today’s day value in first three letters
d is return today’s date value with leading zero.
m is return current month value in numeric format with leading zero.
y is return current year value only last two digit.
h is return current hours value with leading zero in 12 hour format.
i is return current minute value with leading zero.
s is return current second value with leading zero.
a is return am or pm value of time in 12 hours format.

Note: date() function always return machine time. If machine time is set wrong then its return wrong time and date. And also it always returns to Europe/Berlin the time. Because in server setting file php.ini file have set date.timezone=Europe/Berlin.

Set current timezone then in PHP have a function to set current location time zone for particular one application. If anyone want permanently changed timezone the set date.timezone = Asia/Kolkata(or any preferred location) in php.ini file and restart the xampp or wampp.

date_default_timezone_set():

date_default_timezone_set() this function is used to set particular timezone for particular one program where it is run.
Example:

Output:

Date function has any other parameter to return date and time value in different format.

[table id=11 /]

Time function in PHP

time() function is return total time value in the number of second. The Time calculate from 1st January 1970,

Output:

Return date and time using time() function

Return current time as a UNIX timestamp.
Example

Output:

strtotime() in PHP

strtotime() function convert English textual date-time value to a UNIX timestamp. Using this function can return any past or future time or date value.

Output

date_diff() function in PH

The date_diff() is return the difference between to date.

Output:

Explanation
date_create() function return new DateTime object.
%R return + or – sign to denote the difference.
%a returns actual result.