PHP Date and Time

PHP Tutorial

PHP Date and Time Tutorial

In PHP Date and Time provides built-in functions to work with dates and times. You can display, format, manipulate, and calculate date and time values easily using PHP.


 The date() Function

The date() function formats a local date and time according to a specified format.


 

Common Format Characters

CharacterDescriptionExample
Y4-digit year2025
y2-digit year25
mMonth (01-12)11
dDay (01-31)28
HHour (00-23)10
hHour (01-12)10
iMinutes (00-59)15
sSeconds (00-59)30
aam/pmam
lFull weekday nameThursday
DShort weekday nameThu
FFull month nameNovember
MShort month nameNov

Displaying the Current Date and Time


 

Output Example:


 


 Timestamps

A timestamp is the number of seconds since January 1, 1970, 00:00:00 UTC.


 

Convert timestamp to readable date:


 


Date Manipulation with strtotime()

strtotime() converts a string into a timestamp, which you can format using date().


 


 Using mktime()

mktime() creates a timestamp for a specific date and time.


 


 Date Difference


 


 PHP DateTime Class (Object-Oriented)


 


 Summary

  • Use date() to format current date and time.

  • time() gives the current timestamp.

  • strtotime() and mktime() allow date calculations.

  • DateTime class is modern and flexible for date/time manipulation.

  • Always set timezone with date_default_timezone_set().

You may also like...