Display Current Date and Day
1. What are Time Formatting Characters?
In PHP, the date()
function allows you to format and display time using special characters. These characters
represent parts of the time like hours, minutes, seconds, and am/pm.
2. Commonly Used Time Format Characters:
- H: 24-hour format of an hour with leading zeros (00 to 23)
- h: 12-hour format of an hour with leading zeros (01 to 12)
- i: Minutes with leading zeros (00 to 59)
- s: Seconds with leading zeros (00 to 59)
- a: Lowercase Ante meridiem and Post meridiem (am or pm)
3. Example of Using These Characters:
<?php
echo date("h:i:s a");
?>
This will output something like: 03:45:12 pm
4. Combining Characters for Custom Format:
- date("H:i:s") → 24-hour format with full time (e.g., 14:23:45)
- date("h:i a") → 12-hour format with am/pm (e.g., 02:45 pm)
- date("h:i:s a") → 12-hour with seconds and am/pm (e.g., 11:30:00 am)
5. Real-Time Use Case:
When building real-time dashboards, event timers, or digital clocks, these format characters help display time in a readable and user-friendly way.
<!DOCTYPE html>
<html>
<body>
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
</body>
</html>
Output
Today is 2025/03/24
Today is 2025.03.24
Today is 2025-03-24
Today is Monday