Introduction
Date and time functions in PHP help developers display dates, calculate time differences, manage deadlines, and work with timestamps. They are commonly used in attendance systems, employee records, booking applications, and website reports. In this chapter, you will practise Date and Time Functions in PHP important date and time functions through practical solved questions.
Question 1: Display the Current Date and Time Using date()
Problem Statement:
Write a PHP program to display the current date, current time, and current day using the date() function.
PHP Solution:
<?php
date_default_timezone_set("Asia/Kolkata");
echo "Current Date: " . date("d-m-Y") . "\n";
echo "Current Time: " . date("h:i:s A") . "\n";
echo "Current Day: " . date("l");
?>
Sample Output:
Current Date: 04-11-2026
Current Time: 03:05:00 PM
Current Day: Wednesday
The date and time will change according to when the program is executed.
Explanation:
date_default_timezone_set()sets the timezone to Indian Standard Time.date("d-m-Y")displays the date in day-month-year format.date("h:i:s A")displays the time in 12-hour format.date("l")displays the full weekday name.
Concepts Covered: date(), date_default_timezone_set(), date formatting, time formatting.
Question 2: Display Date in Different Formats
Problem Statement:
Write a PHP program to display the same date in different formats, such as 04-11-2026, 2026/11/04, and 04 November 2026.
PHP Solution:
<?php
$date = strtotime("2026-11-04");
echo "Format 1: " . date("d-m-Y", $date) . "\n";
echo "Format 2: " . date("Y/m/d", $date) . "\n";
echo "Format 3: " . date("d F Y", $date) . "\n";
echo "Format 4: " . date("D, d M Y", $date);
?>
Sample Output:
Format 1: 04-11-2026
Format 2: 2026/11/04
Format 3: 04 November 2026
Format 4: Wed, 04 Nov 2026
Explanation:
strtotime()converts the date string into a timestamp.date()converts the timestamp into the required display format.drepresents the day,mrepresents the month number, andYrepresents the four-digit year.Fdisplays the full month name, whileMdisplays its short name.
Concepts Covered: date(), strtotime(), date format characters, timestamps.
Question 3: Get the Current Unix Timestamp Using time()
Problem Statement:
Write a PHP program to display the current Unix timestamp and convert it into a readable date and time.
PHP Solution:
<?php
$timestamp = time();
echo "Unix Timestamp: " . $timestamp . "\n";
echo "Readable Date: " . date("d-m-Y h:i:s A", $timestamp);
?>
Sample Output:
Unix Timestamp: 1793784900
Readable Date: 04-11-2026 03:05:00 PM
The sample timestamp is illustrative. Your program generates the actual current timestamp.
Explanation:
time()returns the current Unix timestamp.- A Unix timestamp represents the number of seconds elapsed since January 1, 1970, in UTC.
- The timestamp is stored in
$timestamp. date()converts it into a readable date and time.
Concepts Covered: time(), Unix timestamp, timestamp conversion.
Question 4: Convert a Text Date into a Timestamp Using strtotime()
Problem Statement:
Write a PHP program to convert a text date into a timestamp and display it in a readable format.
PHP Solution:
<?php
$date = "15 August 2026";
$timestamp = strtotime($date);
echo "Original Date: " . $date . "\n";
echo "Timestamp: " . $timestamp . "\n";
echo "Formatted Date: " . date("d-m-Y", $timestamp);
?>
Sample Output:
Original Date: 15 August 2026
Timestamp: 1786752000
Formatted Date: 15-08-2026
Explanation:
$datestores the date as text.strtotime()converts the recognized date string into a Unix timestamp.- The timestamp can be stored and used for date calculations.
date()converts the timestamp back into a readable format.
Concepts Covered: strtotime(), timestamp conversion, date formatting.
Question 5: Get the Current Date and Time Information Using getdate()
Problem Statement:
Write a PHP program to display the current day, month, year, hour, and minute using getdate().
PHP Solution:
<?php
date_default_timezone_set("Asia/Kolkata");
$dateInfo = getdate();
echo "Day: " . $dateInfo["mday"] . "\n";
echo "Month: " . $dateInfo["month"] . "\n";
echo "Year: " . $dateInfo["year"] . "\n";
echo "Hour: " . $dateInfo["hours"] . "\n";
echo "Minutes: " . $dateInfo["minutes"];
?>
Sample Output:
Day: 4
Month: November
Year: 2026
Hour: 15
Minutes: 5
The output depends on the current date and time.
Explanation:
getdate()returns date and time information as an associative array.mdayrepresents the day of the month.monthrepresents the full month name.yearrepresents the four-digit year.hoursandminutesrepresent the current hour and minute.
Concepts Covered: getdate(), associative arrays, date and time information.
Question 6: Calculate the Number of Days Between Two Dates
Problem Statement:
Write a PHP program to calculate the number of days between two given dates using the DateTime and diff() methods.
PHP Solution:
<?php
$date1 = new DateTime("2026-11-01");
$date2 = new DateTime("2026-11-10");
$difference = $date1->diff($date2);
echo "Number of Days: " . $difference->days;
?>
Sample Output:
Number of Days: 9
Explanation:
DateTimecreates date objects.$date1stores November 1, 2026.$date2stores November 10, 2026.diff()calculates the difference between the two dates.daysreturns the total number of days between them.
Concepts Covered: DateTime, diff(), date difference calculation.
Question 7: Check Whether a Year Is a Leap Year
Problem Statement:
Write a PHP program to check whether the year 2028 is a leap year using the checkdate() function.
PHP Solution:
<?php
$year = 2028;
if (checkdate(2, 29, $year)) {
echo $year . " is a leap year.";
} else {
echo $year . " is not a leap year.";
}
?>
Sample Output:
2028 is a leap year.
Explanation:
checkdate()checks whether a given date is valid.- Its arguments are month, day, and year.
- February 29 is valid in a leap year.
- The program uses this result to identify whether the year is a leap year.
Concepts Covered: checkdate(), date validation, leap year.
Question 8: Add Days to a Given Date Using modify()
Problem Statement:
Write a PHP program to calculate a delivery date by adding 7 days to an order date using the modify() method.
PHP Solution:
<?php
$orderDate = new DateTime("2026-11-04");
$orderDate->modify("+7 days");
echo "Order Date: 04-11-2026\n";
echo "Delivery Date: " . $orderDate->format("d-m-Y");
?>
Sample Output:
Order Date: 04-11-2026
Delivery Date: 11-11-2026
Explanation:
DateTimecreates an object containing the order date.modify("+7 days")adds seven days to that date.format("d-m-Y")displays the calculated delivery date.- The
modify()method changes the existingDateTimeobject.
Concepts Covered: DateTime, modify(), date addition, format().
Question 9: Calculate an Employee’s Joining Experience
Problem Statement:
Write a PHP program to calculate how many years, months, and days an employee has worked in a company. Use a fixed joining date and current date for this example.
PHP Solution:
<?php
$joiningDate = new DateTime("2020-06-15");
$currentDate = new DateTime("2026-11-04");
$experience = $joiningDate->diff($currentDate);
echo "Employee Experience:\n";
echo "Years: " . $experience->y . "\n";
echo "Months: " . $experience->m . "\n";
echo "Days: " . $experience->d;
?>
Sample Output:
Employee Experience:
Years: 6
Months: 4
Days: 20
Explanation:
$joiningDatestores the employee’s joining date.$currentDatestores the date used for calculating experience.diff()calculates the difference between the two dates.y,m, anddreturn the difference in years, months, and days respectively.- In this example, the employee has completed 6 years, 4 months, and 20 days.
Concepts Covered: DateTime, diff(), employee experience calculation.
Question 10: Calculate a Student’s Age Using DateTime
Problem Statement:
Write a PHP program to calculate a student’s age in years, months, and days using their date of birth.
PHP Solution:
<?php
$dateOfBirth = new DateTime("2010-05-20");
$today = new DateTime("2026-11-04");
$age = $dateOfBirth->diff($today);
echo "Date of Birth: 20-05-2010\n";
echo "Age: " . $age->y . " Years, ";
echo $age->m . " Months, ";
echo $age->d . " Days";
?>
Sample Output:
Date of Birth: 20-05-2010
Age: 16 Years, 5 Months, 15 Days
Explanation:
$dateOfBirthstores the student’s birth date.$todaystores the date used for the age calculation.diff()calculates the difference between the two dates.yreturns completed years,mreturns remaining months, anddreturns remaining days.- This approach is useful for calculating student ages and employee records.
Concepts Covered: DateTime, diff(), age calculation, date comparison.
Key Takeaways
date()displays dates and times in different formats.time()returns the current Unix timestamp.strtotime()converts readable date strings into timestamps.getdate()returns date and time information as an array.checkdate()validates a date.DateTimecreates date and time objects.diff()calculates the difference between two dates.modify()adds or subtracts time from a date.format()displays aDateTimeobject in a specified format.date_default_timezone_set()sets the timezone used by PHP date and time functions.
FAQs: Date and Time Functions in PHP
1. What are date and time functions in PHP?
Date and time functions allow developers to display, format, validate, compare, and calculate dates and times in PHP applications.
2. Which function displays the current date in PHP?
The date() function displays the current date using the specified format.
3. What is the purpose of time() in PHP?
The time() function returns the current Unix timestamp, representing the number of seconds since January 1, 1970, in UTC.
4. What is strtotime() in PHP?
strtotime() converts a recognized English date or time string into a Unix timestamp.
5. How can we set the Indian timezone in PHP?
Use the following statement:
date_default_timezone_set("Asia/Kolkata");
6. What is the difference between date() and DateTime in PHP?
date() formats a timestamp into a date or time string. DateTime creates an object that provides methods for formatting, modifying, and comparing dates.
7. How can we calculate the difference between two dates?
Create two DateTime objects and use the diff() method to calculate their difference.
8. How can we check whether a date is valid in PHP?
Use the checkdate() function with the month, day, and year as arguments.
9. How can we add days to a date in PHP?
Use the modify() method with a value such as +7 days to add seven days to a DateTime object.
10. Where are date and time functions used in real-world applications?
They are commonly used in attendance systems, employee records, age calculators, booking systems, appointment schedules, payment deadlines, and report generation.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
