agyanadda@gmail.com

PHP Date and Time

PHP Forms and Web 1 min read Updated Dec 1, 2026 Topic 28 of 36

Set the timezone first

date_default_timezone_set('Asia/Kolkata');

(Or set date.timezone in php.ini, or APP_TIMEZONE in Laravel.)

date() formats

Code Output
d / j day 01–31 / 1–31
m / M / F 12 / Dec / December
Y 2026
D / l Tue / Tuesday
H:i:s 14:05:09
h:i A 02:05 PM

date('d-m-Y') → 01-12-2026 (Indian style).

strtotime()

Turns text into a timestamp: strtotime('+7 days'), strtotime('next Monday').

DateTime (recommended)

$exam = new DateTime('2027-03-15');
$today = new DateTime('today');
$diff = $today->diff($exam);
echo $diff->days . ' days left';

Use DateTimeImmutable so methods like modify('+1 month') return a new object instead of changing the original.

Database format

MySQL stores dates as Y-m-d H:i:s.

Example

Example — Dates, countdown and age PHP
<?php
date_default_timezone_set('Asia/Kolkata');

echo 'Today: ' . date('l, d F Y') . '<br>';
echo 'Time: ' . date('h:i A') . '<br>';
echo 'Indian format: ' . date('d-m-Y') . '<br>';
echo 'Fee due date: ' . date('d M Y', strtotime('+15 days')) . '<br>';

$today = new DateTimeImmutable('today');
$exam = new DateTimeImmutable(date('Y') . '-12-31');
$days = $today->diff($exam)->days;
echo "Days left this year: $days<br>";

$dob = new DateTimeImmutable('2004-08-15');
echo 'Age: ' . $dob->diff($today)->y . ' years<br>';

$next = $today->modify('+1 month');
echo 'Next payment: ' . $next->format('d/m/Y');

Output (on 1 December 2026, morning): Today: Tuesday, 01 December 2026 / Time: 10:30 AM / Indian format: 01-12-2026 / Fee due date: 16 Dec 2026 / Days left this year: 30 / Age: 22 years / Next payment: 01/01/2027

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which timezone should Indian websites use?

Exercise 2 True / False

date('Y') returns the four-digit year.

Exercise 3 Fill in the Blank

___('+7 days') converts text into a future timestamp.

Frequently Asked Questions

PHP is using UTC. Set date_default_timezone_set('Asia/Kolkata') or the timezone in php.ini.