🔴 Advanced · Lesson 29
Date and Time
Working with Dates
PHP makes it easy to show, format and calculate dates using
date(), strtotime() and the DateTime class.Common Examples
echo date("Y-m-d"); // 2026-06-03
echo date("d/m/Y H:i"); // 03/06/2026 14:30
echo date("l"); // Wednesday
$ts = strtotime("2026-01-01");
echo date("d M Y", $ts); // 01 Jan 2026
Date Difference
$d1 = new DateTime("2026-01-01");
$d2 = new DateTime("2026-06-03");
$diff = $d1->diff($d2);
echo $diff->days . " days"; // 153 days
Summary
date()formats the current/any date;strtotime()parses text to a timestamp.DateTimewith diff() calculates differences between dates.
Dates के साथ काम करना
PHP
date(), strtotime() और DateTime class से dates दिखाना, format करना और calculate करना आसान बनाता है।Common Examples
echo date("Y-m-d"); // 2026-06-03
echo date("d/m/Y H:i"); // 03/06/2026 14:30
echo date("l"); // Wednesday
$ts = strtotime("2026-01-01");
echo date("d M Y", $ts); // 01 Jan 2026
Date Difference
$d1 = new DateTime("2026-01-01");
$d2 = new DateTime("2026-06-03");
$diff = $d1->diff($d2);
echo $diff->days . " days"; // 153 days
सारांश
date()current/कोई भी date format करता है;strtotime()text को timestamp बनाता है।DateTimeका diff() dates के बीच अंतर निकालता है।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.