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.
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
?>
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
| Letter | Means | Example |
|---|---|---|
| Y | 4-digit year | 2026 |
| m | Month (01-12) | 01 |
| d | Day (01-31) | 15 |
| H:i:s | Hour:Min:Sec | 14:30:00 |
| l | Day name | Thursday |
time() and timestamps
<?php
$now = time(); // current timestamp (seconds)
$tomorrow = $now + 86400; // + 1 day (60*60*24)
echo date("Y-m-d", $tomorrow);
?>
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");
?>
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";
?>
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.
mis month,Mis a short month name,dis 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
DateTimeandmodify()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
DateTimeclass 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?
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?
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?
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?
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.