Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 29

Date and Time in PHP

In this lesson: Format dates in PHP with date(), set timezone (Asia/Kolkata), strtotime and DateTime diff — examples with output.

Working with date and time

Almost every app shows dates — a post date, an order time, an age. PHP gives you a quick function, date(), for formatting, and a powerful class, DateTime, for calculations. Learn both and you can handle any date task.

💡 Set your time zone first with date_default_timezone_set("Asia/Kolkata") so times match your region.

The date() function

<?php
  date_default_timezone_set("Asia/Kolkata");
  echo date("Y-m-d");      // 2026-01-15
  echo date("H:i:s");      // 14:30:00
  echo date("l");          // Thursday
?>
2026-01-15 14:30:00 Thursday

The string you pass tells date() what to output. Each letter is a format code that PHP replaces with the matching part of the current date.

Common format letters

LetterMeansExample
Y4-digit year2026
mMonth (01-12)01
dDay (01-31)15
H:i:sHour:Min:Sec14:30:00
lDay nameThursday

time() and timestamps

<?php
  $now = time();               // current timestamp (seconds)
  $tomorrow = $now + 86400;    // + 1 day (60*60*24)
  echo date("Y-m-d", $tomorrow);
?>
2026-01-16

A timestamp is just a number of seconds, so adding a day means adding 86400. Pass a timestamp as the second argument to date() to format that moment.

The DateTime class

For richer work, use the object-oriented DateTime.

<?php
  $d = new DateTime("2026-01-15");
  $d->modify("+1 week");
  echo $d->format("Y-m-d");
?>
2026-01-22

DateTime understands human phrases like "+1 week" and formats with format(), the same codes as date().

Date difference

<?php
  $d1 = new DateTime("2026-01-01");
  $d2 = new DateTime("2026-01-15");
  $diff = $d1->diff($d2);
  echo $diff->days . " days";
?>
14 days

diff() returns a DateInterval. Its ->days gives the total days between the two dates — perfect for ages, durations and deadlines.

Common mistakes

  • Wrong time zone. Without setting it, times may be hours off. Set it at the top of the script.
  • Mixing up format letters. m is month, M is a short month name, d is day — check the code.
  • Forgetting the timestamp argument. To format a specific time, pass it as the second argument to date().
  • Doing date maths by hand. Use DateTime and modify() instead of manual second counting.

Summary

  • date("Y-m-d") formats the current date; letters are format codes.
  • time() gives a timestamp (seconds since 1970).
  • The DateTime class handles adding, comparing and formatting.
  • diff() finds the gap between two dates.
  • Always set the time zone for correct local times.

Next, format strings alongside dates with PHP string functions, or store dates in MySQL date types.

Frequently Asked Questions

How do I get the current date in PHP?
Use date("Y-m-d") for the date and date("H:i:s") for the time. The letters inside are format codes — Y is a four-digit year, m is the month, d is the day, and so on.
What is a timestamp in PHP?
A timestamp is the number of seconds since 1 January 1970 (the Unix epoch). time() gives the current timestamp. Timestamps are handy for doing maths with dates, like adding days.
What is the difference between date() and the DateTime class?
date() is a quick function for formatting. The DateTime class is an object-oriented approach that is better for complex work — adding intervals, comparing dates, and handling time zones cleanly.
How do I find the number of days between two dates?
Create two DateTime objects and use $d1->diff($d2). The result is a DateInterval, and ->days gives the total number of days between them.
How do I set the correct time zone?
Call date_default_timezone_set("Asia/Kolkata") at the top of your script, or set it in php.ini. This ensures dates and times match your region instead of the server default.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 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.
Next topicRegular Expressions Your progress: 29 / 50 (58%) · Lesson 30 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free