← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 1 of 24

PHP Date & Time

PHP's date and time tools work like a calendar and clock combined. You can ask for today's date, show it in any style you like, or work out what day it will be in a week.
Syntax
php
echo date("Y-m-d H:i:s");                // format the current time
$timestamp = strtotime("+3 days");
$dt = new DateTime("date string");
echo $dt->format("Y-m-d");

Getting the Current Date and Time

The date() function formats the current time (or any Unix timestamp you pass it) according to a format string of letter codes, so date(Y-m-d) gives you a sortable ISO-style date instantly.

Example: Getting the Current Date and Time

php
<?php
// Print `date('Y-m-d')` to the output
echo date('Y-m-d');
?>

Creating Timestamps from Strings

strtotime() parses everyday English phrases like 'next Monday' or '+3 days' into a Unix timestamp, which is remarkably flexible for turning user-typed dates into something PHP can calculate with.

Example: Creating Timestamps from Strings

php
<?php
// Declare `$timestamp`, set to `strtotime("+3 days")`
$timestamp = strtotime("+3 days");
// Print `date('Y-m-d', $timestamp)` to the output
echo date('Y-m-d', $timestamp);
?>

Working with the DateTime Class

The DateTime class wraps date handling in an object-oriented API that correctly accounts for tricky edge cases like leap years and daylight-saving shifts, which manual timestamp math tends to get wrong.

Example: Working with the DateTime Class

php
<?php
// Create a new `DateTime` instance with '2024-02-29', stored in `$date`
$date = new DateTime('2024-02-29');
// Print `$date->format('Y-m-d')` to the output
echo $date->format('Y-m-d');
?>

Modifying Dates

DateTime's modify() method and the separate DateInterval class both let you shift a date forward or backward by a readable amount (like '+1 month'), which is far less error-prone than adding raw seconds.

Example: Modifying Dates

php
<?php
// Create a new `DateTime` instance with '2024-01-01', stored in `$date`
$date = new DateTime('2024-01-01');
$date->modify('+1 month');
// Print `$date->format('Y-m-d')` to the output
echo $date->format('Y-m-d');
?>

Timezones in PHP

date_default_timezone_set() fixes the timezone your whole script assumes when it isn't told otherwise; without it, PHP falls back to a server default that may not match your users' expectations.

Example: Timezones in PHP

php
<?php
// Call `date_default_timezone_set('America/New_York')`
date_default_timezone_set('America/New_York');
// Print `date('Y-m-d H:i:s')` to the output
echo date('Y-m-d H:i:s');
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not setting the time zone with date_default_timezone_set, so dates use an unexpected default.
  2. Confusing m (month) and i (minutes) in date format strings, such as Y-m-d H:m:s.
  3. Assuming DateTime::modify returns a new object, when it changes the object in place; use DateTimeImmutable to avoid that.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.