PHP Date and Time ⏳
PHP provides functions to work with dates and times, making it easy to display and manipulate them.
Display Current Date & Time (date()
)
✔ Y-m-d → Year-Month-Day
✔ H:i:s → Hour:Minute:Secondecho date("Y-m-d H:i:s"); // Output: 2025-02-28 14:30:00
✔ Y-m-d
→ Year-Month-Day
✔ H:i:s
→ Hour:Minute:Second
Get Current Timestamp (time()
)
echo time(); // Output: 1709112000 (Unix timestamp)
✔ Returns seconds since January 1, 1970 (Unix Epoch).
Format Custom Date & Time
echo date("l, F j, Y"); // Output: Friday, February 28, 2025
✔ l
→ Full weekday name
✔ F
→ Full month name
✔ j
→ Day without leading zero
Convert Timestamp to Date (date()
)
$timestamp = 1672524000;
echo date("Y-m-d", $timestamp); // Output: 2023-01-01
✔ Converts Unix timestamp to a readable date.
Get Individual Date Parts
echo date("d"); // Day (01-31)
echo date("m"); // Month (01-12)
echo date("Y"); // Year (e.g., 2025)
Conclusion
✔ date()
formats dates.
✔ time()
gets Unix timestamps.
Mastering date & time is crucial for scheduling, logs, and event tracking! 🚀