PHP has solid tools for working with dates and times. Whether you need to display today's date, calculate when a user's subscription expires, or format a timestamp for a database, PHP's got you covered with date(), time(), strtotime(), and the DateTime class.
date() — Formatting the Current Date
The date() function takes a format string and an optional timestamp. If you don't give it a timestamp, it uses the current time. The format characters are where the real power is: Y for four-digit year, m for month with leading zero, d for day, H for hour, i for minutes, and s for seconds.
<?php
echo date("Y-m-d H:i:s");
echo date("l, F j, Y");
?>
Try it Yourself →
time() and strtotime()
time() returns the current Unix timestamp — the number of seconds since January 1, 1970. It's useful for calculations. strtotime() is the reverse: it takes a human-readable string like "next Monday" or "+2 weeks" and turns it into a timestamp.
<?php
echo time();
echo strtotime("next Sunday");
echo strtotime("+1 month");
?>
Try it Yourself →
The DateTime Class
For serious date work, use the DateTime class. It lets you create date objects, modify them, compare them, and format them. It handles timezones and complex operations way better than the procedural functions.
<?php
$date = new DateTime();
echo $date->format("Y-m-d H:i:s");
$date->modify("+1 week");
echo $date->format("Y-m-d");
?>
Try it Yourself →