PHP provides powerful built-in functions to get, format, and manipulate date and time. These functions are widely used in logging systems, user activities, scheduling, and dynamic websites.
Always set the correct timezone before working with date and time.
The date() function is used to format the current date and time.
| Character | Description | Example |
|---|---|---|
| Y | 4-digit year | 2025 |
| y | 2-digit year | 25 |
| m | Month (01-12) | 07 |
| d | Day (01-31) | 21 |
| H | Hour (24-hour) | 18 |
| h | Hour (12-hour) | 06 |
| i | Minutes | 30 |
| s | Seconds | 45 |
| A | AM / PM | PM |
time() FunctionReturns the current timestamp (seconds since Jan 1, 1970).
strtotime()Convert a date string into a timestamp.
Using DateTime and DateInterval.
Showing current date/time
User activity logs
Expiry dates
Countdown timers
Event scheduling
Always set timezone
Use DateTime class for complex operations
Store dates in Y-m-d H:i:s format in database
Convert dates only when displaying
PHP date and time functions make it easy to manage and manipulate time-based data. Mastering these functions is essential for building dynamic and time-aware web applications.
Take quizzes related to this topic and see where you stand!
Start Quiz Now