PHP Date and Time


Working with date and time is a common task in web development. PHP provides a powerful set of built-in functions that let you display, format, and manipulate dates and times easily. Whether you’re creating a blog, a user dashboard, or an attendance system, understanding how PHP handles date and time is essential.

Why Date and Time Matter

In most web applications, you need to record when something happens — such as when a user signs up, posts a comment, or uploads a file. Using PHP’s date and time functions, you can automatically generate timestamps, format them for display, and even calculate differences between two dates.

For example, if Vrinda registers on your website, PHP can store the exact date and time of her registration. Later, you can display it as “Joined on 09 October 2025 at 10:30 PM”.

Displaying the Current Date and Time

The easiest way to display the current date and time in PHP is by using the date() function.

<?php
echo date("Y-m-d H:i:s");
// Example output: 2025-10-09 22:15:32
?>

Explanation:

  • Y – Full year (2025)

  • m – Month (01 to 12)

  • d – Day of the month (01 to 31)

  • H – Hour in 24-hour format

  • i – Minutes

  • s – Seconds

You can change the format as needed. For example:

<?php
echo date("l, F j, Y");
// Output: Thursday, October 9, 2025
?>

Here, l gives the day name and F gives the full month name.

Setting the Time Zone

PHP uses the server’s default timezone. But if your users are from different locations, you might need to change it. You can do that using date_default_timezone_set().

<?php
date_default_timezone_set("Asia/Kolkata");
echo "Current time in India: " . date("h:i A");
?>

If Ananya is in India, this code ensures the time displayed matches her local time.

You can find a list of supported time zones in PHP’s official documentation.

Getting the Current Timestamp

A timestamp is a numeric value representing the number of seconds since January 1, 1970 (known as the Unix Epoch). You can get the current timestamp with time().

<?php
echo time();
// Example output: 1739091332
?>

This timestamp is useful for calculations like determining how many days have passed since a user joined.

Formatting a Timestamp

You can convert a timestamp into a readable date using date().

<?php
$timestamp = time();
echo date("d-m-Y H:i:s", $timestamp);
?>

If Riya uploads a project today, you can store time() in the database and later show it in any format you like.

Creating Custom Dates with mktime()

The mktime() function lets you create your own timestamp by providing the hour, minute, second, month, day, and year.

<?php
$customDate = mktime(9, 30, 0, 10, 10, 2025);
echo "Custom Date: " . date("Y-m-d H:i:s", $customDate);
?>

This will output 2025-10-10 09:30:00.
It’s handy when you want to calculate future or past dates — for example, the expiry date of a membership.

Working with the DateTime Object

While date() and time() work fine for most tasks, PHP also provides the DateTime class for more advanced operations.

<?php
$date = new DateTime("2025-10-09 10:00:00");
$date->modify("+2 days");
echo $date->format("Y-m-d H:i:s");
?>

This changes the date to 2025-10-11 10:00:00.
For example, Nisha books an event two days from now — this method helps you calculate the exact future date.

Comparing Two Dates

You can compare two dates using timestamps or the DateTime object.

<?php
$date1 = strtotime("2025-10-09");
$date2 = strtotime("2025-10-15");

$diff = $date2 - $date1;
$days = $diff / (60 * 60 * 24);

echo "Difference: $days days";
?>

If Vrinda joins on October 9 and Riya joins on October 15, the output will show “Difference: 6 days”.

Displaying Relative Dates

PHP can handle relative date formats too. You can use strings like “+1 week” or “-2 days”.

<?php
$nextWeek = strtotime("+1 week");
echo "Next Week: " . date("Y-m-d", $nextWeek);
?>

You can also calculate a past date:

<?php
$lastMonth = strtotime("-1 month");
echo "Last Month: " . date("Y-m-d", $lastMonth);
?>

This helps when building features like “recent activity” or “upcoming events.”

Practical Example: Displaying User Join Date

Let’s create a small example where Vrinda registers on your website, and you display her join date.

<?php
date_default_timezone_set("Asia/Kolkata");

$name = "Vrinda";
$joinDate = date("l, d F Y h:i A");

echo "$name joined on $joinDate.";
?>

Output:
Vrinda joined on Thursday, 09 October 2025 10:30 PM.

This is how PHP helps you record and display time-based data automatically.

Common Mistakes to Avoid

  1. Forgetting to set timezone:
    Dates may appear wrong if your server uses a different timezone.

  2. Using wrong date format characters:
    For example, using lowercase y instead of uppercase Y gives a two-digit year.

  3. Not converting timestamps properly:
    Always use date() or DateTime::format() to display timestamps.

Summary of the Tutorial

PHP makes it simple to manage date and time — from displaying the current moment to handling complex time calculations.
You can:

  • Show current time with date()

  • Adjust timezone with date_default_timezone_set()

  • Get timestamps with time()

  • Format and compare dates with strtotime() and DateTime

Whether you’re tracking when a blog post was published or calculating the number of days until an event, these functions make date and time handling efficient and reliable.


Practice Questions

  1. Display the current date and time in the format Day, Month Date Year - Hour:Minute:Second.

  2. Set the timezone to Asia/Kolkata and print the current time in 12-hour format with AM/PM.

  3. Show today’s date in the format d/m/Y and yesterday’s date in the same format.

  4. Get the current Unix timestamp and convert it into a readable date and time format.

  5. Use mktime() to create a timestamp for 15th August 2025, 10:00 AM, and display it in Y-m-d H:i:s format.

  6. Calculate and display the number of days between 2025-10-01 and today’s date.

  7. Display the date for one week from now and one month ago using strtotime().

  8. Create a PHP script that displays the current date and time for three different time zones: Asia/Kolkata, Europe/London, and America/New_York.

  9. Use the DateTime class to add 5 days to the current date and display the new date.

  10. Display a message like “Vrinda joined on [current date and time]” using PHP’s date() function.


Go Back Top