Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

27. Date & Time Functions

Lesson 24 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Date and Time Functions in PHP

Date and time functions are used to display, calculate, format, and manipulate date and time values in PHP applications such as login systems, reports, logs, and bookings.

Exam Definition: Date and time functions are used to handle and manipulate date and time values in PHP.

---

Setting Time Zone

Before working with date/time, set the correct time zone.

date_default_timezone_set("Asia/Kolkata");
Exam Tip: Time zone should be set to avoid incorrect time.

---

date() Function

Used to format date and time.

Syntax

date(format, timestamp);

Example

echo date("d-m-Y");

Common Date Format Characters

FormatMeaning
dDay (01–31)
mMonth (01–12)
YYear (4-digit)
HHour (24-hour)
iMinutes
sSeconds

Example: Date & Time

echo date("d-m-Y H:i:s");

---

time() Function

Returns current timestamp (seconds since 1 Jan 1970).

echo time();
Exam Line: Timestamp is the number of seconds since Unix Epoch.

---

mktime() Function

Creates a timestamp for a specific date/time.

$ts = mktime(10, 30, 0, 5, 20, 2026);
echo date("d-m-Y H:i:s", $ts);

---

strtotime() Function

Converts string to timestamp.

echo date("d-m-Y", strtotime("next Monday"));
Real-time Use: Expiry date calculation.

---

DateTime Class (Modern Way)

$date = new DateTime();
echo $date->format("d-m-Y");

Date Difference

$d1 = new DateTime("2025-01-01");
$d2 = new DateTime("2026-01-01");
$diff = $d1->diff($d2);
echo $diff->days;

Common Date & Time Functions Summary

FunctionPurpose
date()Format date
time()Current timestamp
mktime()Custom timestamp
strtotime()String → timestamp
DateTimeObject-oriented date