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



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() this function is used to set particular timezone for particular one program where it is run.


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

Sr No

jReturn date value without leading zero

lReturn day value with full textual representation
Sunday to Saturday
Return day value with numeric representation
1 to 7 (Monday to Sunday)
Return English ordinal suffix for the day of month in two charactersst, nd, rd or th
Return day value with numeric representation0 to 6(Sunday to Saturday)
Return day value of the year0 through 365
Return week value in numeric format of year42nd week in the year
Return month value with textual representationJanuary to December
Return month value with text format only three lettersJan to Dec
Return month value in numeric format without leading zero1 to 12
Return number of days in the current month
28 or 30 or 31
Return current year is leap year or notIf leap year the return 1 otherwise 0
Return current year value with four digit2000 or 2018
Return am or pm in uppercase AM or PM
AM or PM
Return 12 hour format of an hour without leading zeros1 to 12

Return 24 hour format of an hour without leading zeros1 to 24
HReturn 24 hour format of an hour with leading zeros01 to 24

Time function in PHP

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


Return date and time using time() function

Return current time as a UNIX timestamp.


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.


date_diff() function in PH

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


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