PHP Date and Time

Almost every real app needs to show, store, or calculate dates: "posted 3 days ago", invoice due dates, scheduling, log timestamps. PHP gives you two ways to work with dates — the classic date() function and the object-oriented DateTime class. This page covers both, plus strtotime() and timezones.

1) The date() Function

date() formats the current time (or a given Unix timestamp) into a string, using format characters that each stand for a piece of the date.

<?php
  echo date('Y-m-d');       // e.g. 2026-08-14
  echo date('H:i:s');        // e.g. 14:05:09
  echo date('l, d F Y');   // e.g. Friday, 14 August 2026
?>
Common format characters
CharacterMeaningExample
Y4-digit year2026
y2-digit year26
mMonth, zero-padded08
dDay of month, zero-padded14
lFull weekday nameFriday
DShort weekday nameFri
FFull month nameAugust
H24-hour, zero-padded14
g12-hour, no padding2
iMinutes, zero-padded05
sSeconds, zero-padded09
AAM/PM (uppercase)PM

2) Live Demo: The Server's Real Current Time

The values below are computed with date() the instant this page renders on the server — reload the page and the seconds will change.

Format stringLive output right now
Y-m-d2026-10-27
Y-m-d H:i:s2026-10-27 10:50:55
l, d F YTuesday, 27 October 2026
D M j, YTue Oct 27, 2026
g:i A10:50 AM
H:i:s10:50:55

3) time() — The Current Unix Timestamp

time() returns the number of seconds since 1 January 1970 (the "Unix epoch"). It's the raw number that date() formats.

<?php
  $now = time();
  echo $now;              // e.g. 1786723200
  echo date('Y-m-d', $now);  // format that timestamp
?>
Right now, time() on this server returns 1793098255.

4) strtotime() — Parsing Human-Readable Strings

strtotime() converts almost any English date phrase into a Unix timestamp, which you can then format with date().

<?php
  echo date('Y-m-d', strtotime('next monday'));
  echo date('Y-m-d', strtotime('+1 week'));
  echo date('Y-m-d', strtotime('-3 days'));
  echo date('Y-m-d', strtotime('first day of next month'));
?>
strtotime() phraseLive result
next monday2026-11-02 (Monday)
+1 week2026-11-03
-3 days2026-10-24
first day of next month2026-11-01

5) The DateTime Class (Object-Oriented)

For anything more than simple formatting, the DateTime class is easier to work with — it lets you modify, compare, and diff dates as objects instead of juggling timestamps.

<?php
  $date = new DateTime();          // now
  echo $date->format('Y-m-d H:i:s');

  $date->modify('+10 days');
  echo $date->format('Y-m-d');   // 10 days from now
?>

6) Setting a Timezone

All date/time functions use a default timezone. Set it once, near the top of your app, with date_default_timezone_set(). This exact site does that already in track-visitor.php:

<?php
  date_default_timezone_set("Asia/Kolkata");
?>
Because that line already runs (via track-visitor.php, included in this page's <head>), every timestamp on this page is genuinely in Indian Standard Time — not UTC.

7) Calculating a Difference with DateTime::diff()

DateTime::diff() compares two DateTime objects and returns a DateInterval with years/months/days/hours between them — much safer than subtracting timestamps by hand.

<?php
  $today   = new DateTime('today');
  $newYear = new DateTime('first day of January next year');
  $diff    = $today->diff($newYear);
  echo $diff->days . ' days until New Year';
?>
CalculationLive result
Days until next New Year's Day (from today, 2026-10-27) 66 days (target: 2027-01-01)
Days since 2024-01-01 1030 days
Rule of thumb: use date()/strtotime() for quick, one-off formatting; switch to the DateTime class once you need to modify, compare, or diff dates — the code stays far more readable.