PHP Date and Time

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
| Character | Description | Example |
|---|---|---|
Y | 4-digit year | 2025 |
y | 2-digit year | 25 |
m | Month (01-12) | 11 |
d | Day (01-31) | 28 |
H | Hour (00-23) | 10 |
h | Hour (01-12) | 10 |
i | Minutes (00-59) | 15 |
s | Seconds (00-59) | 30 |
a | am/pm | am |
l | Full weekday name | Thursday |
D | Short weekday name | Thu |
F | Full month name | November |
M | Short month name | Nov |
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()andmktime()allow date calculations.DateTimeclass is modern and flexible for date/time manipulation.Always set timezone with
date_default_timezone_set().
