Home » Programming » php » PHP Date and Time

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.


Sr No

Parameters
MeaningOutput
jReturn date value without leading zero
1to31

lReturn day value with full textual representation
Sunday to Saturday
N
Return day value with numeric representation
1 to 7 (Monday to Sunday)
S
Return English ordinal suffix for the day of month in two characters st, nd, rd or th
w
Return day value with numeric representation0 to 6(Sunday to Saturday)
z
Return day value of the year0 through 365
W
Return week value in numeric format of year42nd week in the year
F
Return month value with textual representationJanuary to December
M
Return month value with text format only three lettersJan to Dec
n
Return month value in numeric format without leading zero1 to 12
t
Return number of days in the current month
28 or 30 or 31
L
Return current year is leap year or notIf leap year the return 1 otherwise 0
Y
Return current year value with four digit 2000 or 2018
A
Return am or pm in uppercase AM or PM
AM or PM
g
Return 12 hour format of an hour without leading zeros1 to 12
G

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,

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.

Please follow and like us:
error