Working with dates and times is an essential skill for web development. Whether you are creating a blog, a booking system, or a social media platform, you will need to display and manipulate timestamps.
PHP provides a powerful date() function to format dates and times efficiently.
The date() function formats a timestamp into a more readable date and time.
Syntax:
date(format, timestamp)
The format parameter specifies how the date should be returned. Here are a few common characters used for dates:
d: Represents the day of the month (01 to 31)m: Represents a month (01 to 12)Y: Represents a year (in four digits)l (lowercase 'L'): Represents the day of the week
<?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");
?>
You can also extract the current time. Here are common characters for time:
H: 24-hour format of an hour (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)(Note: If the time returned from your code is incorrect, it's likely because your server is in a different timezone. You can set your specific timezone using date_default_timezone_set("America/New_York"); before calling the date function.)
Which character is used in the date() function to represent a four-digit year?