PHP Date and Time

PHP Date and Time

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 PHP date() Function

The date() function formats a timestamp into a more readable date and time.

Syntax:

date(format, timestamp)

Formatting a Date

The format parameter specifies how the date should be returned. Here are a few common characters used for dates:

Date Example

<?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");
?>

Formatting a Time

You can also extract the current time. Here are common characters for time:

(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.)


Exercise

?

Which character is used in the date() function to represent a four-digit year?