Call date('Y-m-d H:i:s'): it returns the current date and time as a string such as 2026-12-01 16:42:51, the same layout as MySQL's NOW(). The current year is date('Y'). When you need to add, compare or convert the time, use new DateTimeImmutable() and its format() method instead. Both use the default time zone, so set it with date_default_timezone_set() if UTC is not what you want.
PHP gives you the clock in three shapes: a formatted string from date(), an object from DateTimeImmutable, and a number from time(), microtime() or hrtime(). Which one you want depends on what you do next: print it, compute with it, store it, or time a piece of code. The sections below cover the format letters and the current year, the object API and its mutable twin's trap, time zones, timestamps in seconds and milliseconds, the strings MySQL and JSON APIs expect, and a clock you can freeze in tests, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.
1date() and the format lettersRecommended
date($format) returns the current time as a string built from a format. Each letter it knows is replaced by part of the date (Y the four-digit year, m the month with a leading zero, d the day, H, i and s the time); every other character is copied as is, and a backslash makes a letter literal. The current year is just date('Y'), a string; idate('Y') returns it as an int. Pass a Unix timestamp as the second argument to format that moment instead of now.
<?php // The current date and time as a string echo date('Y-m-d H:i:s'), "\n"; // Just the year: a string from date(), an int from idate() echo date('Y'), "\n"; var_dump(idate('Y')); // Letters are replaced, anything else is copied as is echo date('D, d M Y'), "\n"; // short day, day, short month, year echo date('l, F jS'), "\n"; // full day, full month, day + suffix echo date('g:i A'), "\n"; // 12-hour clock echo date('H:i:s'), "\n"; // 24-hour clock echo date('\T\o\d\a\y \i\s l'), "\n"; // a backslash keeps a letter literal // A second argument formats that Unix timestamp instead of now echo date('Y-m-d H:i:s', 0), "\n"; echo date('l jS F Y g:i A', 1782907200), "\n";
Output
A run on Dec 1, 2026 printed 2026-12-01 16:42:51, 2026 and int(2026), then Tue, 01 Dec 2026, Tuesday, December 1st, 4:42 PM and Today is Tuesday; yours shows the moment you press Run. The last two lines never change: timestamp 0 is 1970-01-01 00:00:00, and 1782907200 is Wednesday 1st July 2026 12:00 PM. The time is in UTC because that is the default zone here (section 3). Watch the case of the letters: m is the month but i is minutes, H is the 24-hour clock and h the 12-hour one. To turn an existing date string into another layout, see formatting a date.
2DateTimeImmutable for dates you compute with
A string is the end of the road. When you also need tomorrow's date, the start of the month or a comparison, create the current time as an object with new DateTimeImmutable() (the same as passing 'now') and call format() with the same letters as date() when you print it. PHP has two such classes. DateTimeImmutable returns a new object from modify(), add() and setTime(); DateTime changes the object itself, which is the source of a classic bug shown at the end.
<?php $now = new DateTimeImmutable(); // same as new DateTimeImmutable('now') echo $now->format('Y-m-d H:i:s'), "\n"; echo 'Year: ', $now->format('Y'), "\n"; // Every change returns a new object; $now stays as it was $tomorrow = $now->modify('+1 day'); $inAnHour = $now->add(new DateInterval('PT1H')); echo 'Tomorrow: ', $tomorrow->format('Y-m-d'), "\n"; echo 'Midnight: ', $now->setTime(0, 0)->format('Y-m-d H:i:s'), "\n"; $first = $now->modify('first day of this month'); echo 'Month start: ', $first->format('Y-m-d'), "\n"; var_dump($inAnHour > $now); // The trap: DateTime changes the object you call modify() on $start = new DateTime('2026-12-01 09:00'); $end = $start->modify('+2 hours'); echo 'DateTime: ', $start->format('H:i'), ' to ', $end->format('H:i'), "\n"; $start = new DateTimeImmutable('2026-12-01 09:00'); $end = $start->modify('+2 hours'); echo 'DateTimeImmutable: ', $start->format('H:i'), ' to ', $end->format('H:i'), "\n";
Output
On Dec 1, 2026 the run gave 2026-12-02 for tomorrow, 2026-12-01 00:00:00 for midnight and 2026-12-01 for the month start, and objects compare with > and < directly, so $inAnHour > $now is bool(true). The last two lines are the trap: the DateTime line reads 11:00 to 11:00. DateTime::modify() moved $start itself and returned the same object, so the start time is gone. With DateTimeImmutable it prints 09:00 to 11:00. Use the immutable class by default and you never hit this. To measure the gap between two of these objects, see calculating the difference between two dates.
3Time zones
date() and new DateTimeImmutable() both use the default time zone, which comes from the date.timezone setting in php.ini. Set it there (for example date.timezone = Europe/London) or at the top of the script with date_default_timezone_set(); the examples here run in UTC. To show a time in another zone without changing the default, pass a DateTimeZone to the constructor or call setTimezone(). The multi-zone lines below use a fixed timestamp, so they print the same on every run.
<?php // date.timezone decides what "now" looks like; this lane uses UTC echo 'Default: ', date_default_timezone_get(), "\n"; echo date('Y-m-d H:i T'), "\n"; // Change the default for the rest of the script date_default_timezone_set('America/New_York'); echo date('Y-m-d H:i T'), "\n"; // Or give one object its own zone and leave the default alone $tokyo = new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo')); echo $tokyo->format('Y-m-d H:i T (P)'), "\n"; // One fixed instant (a Unix timestamp) shown in several zones $instant = new DateTimeImmutable('@1782907200'); $zones = ['UTC', 'America/New_York', 'Europe/London', 'Asia/Kolkata']; foreach ($zones as $zone) { $local = $instant->setTimezone(new DateTimeZone($zone)); printf("%-17s %s\n", $zone, $local->format('Y-m-d H:i T (P)')); } // A misspelled zone throws instead of silently using UTC try { new DateTimeZone('America/NewYork'); } catch (Exception $e) { echo get_class($e), ': ', $e->getMessage(), "\n"; }
Output
On Dec 1, 2026 the "now" lines read 2026-12-01 16:42 UTC, 2026-12-01 11:42 EST and 2026-12-02 01:42 JST (+09:00): one moment, three wall clocks, and Tokyo is already on the next day. The fixed instant is 12:00 UTC, 08:00 EDT in New York, 13:00 BST in London and 17:30 IST in Kolkata, with daylight saving applied by date. A bad zone name throws DateInvalidTimeZoneException (PHP 8.3 and later; older versions throw a plain Exception, which the catch above also covers), while date_default_timezone_set() with a bad name returns false and prints a Notice. The old warning date(): It is not safe to rely on the system's timezone settings came from PHP 5 when date.timezone was unset; PHP 7.0 dropped it and uses UTC, which is why server times can look hours off without any warning.
4Unix timestamps and milliseconds
A Unix timestamp counts seconds since 1970-01-01 00:00:00 UTC, so it is the same number in every time zone. time() returns it as an int. microtime(true) returns a float with microseconds; multiply by 1000 and take (int) floor() for the millisecond count JavaScript's Date.now() uses. To measure how long code takes, use hrtime(true) (PHP 7.3 and later): nanoseconds from a monotonic clock that does not jump when the system time is corrected.
<?php // Seconds since 1970-01-01 00:00:00 UTC printf("%-13s %s\n", 'time()', time()); // A float with microseconds; echo shows 14 significant digits printf("%-13s %s\n", 'microtime()', microtime(true)); // Milliseconds as an int, like JavaScript's Date.now() $ms = (int) floor(microtime(true) * 1000); printf("%-13s %s\n", 'milliseconds', $ms); // From an object: U is seconds, v is milliseconds, u is microseconds $now = new DateTimeImmutable(); printf("%-13s %s\n", "format('Uv')", $now->format('Uv')); printf("%-13s %s\n", 'with .v', $now->format('Y-m-d H:i:s.v')); printf("%-13s %s\n", 'with .u', $now->format('Y-m-d H:i:s.u')); // And back: milliseconds to a date $back = new DateTimeImmutable('@' . 1782907200123 / 1000); printf("%-13s %s\n", 'from ms', $back->format('Y-m-d H:i:s.v')); // Time code with hrtime(): nanoseconds, never goes backwards $t0 = hrtime(true); usleep(20_000); $elapsedMs = (hrtime(true) - $t0) / 1_000_000; printf("%-13s %.1f ms\n", 'slept', $elapsedMs);
Output
One run printed 1796143375 for time() and 1796143375.0293 for microtime(true): only four decimals show because echo and %s round floats to the precision setting (14 digits), though the value holds more. The milliseconds value is a 13-digit int such as 1796143375029; without the cast, microtime(true) * 1000 stays a float (float(1796143226477.7961) in a test run). format('Uv') gives the same digits as a string, and .v and .u add milliseconds or microseconds to a readable date. Going back, 1782907200123 ms is 2026-07-01 12:00:00.123. The last line is a little over 20 ms (20.1 ms in that run) and varies on every run.
5A MySQL NOW() string and ISO 8601
PHP has no NOW() function; calling now() ends the script with Call to undefined function now(). The equivalent of MySQL's NOW() is the format 'Y-m-d H:i:s', which a DATETIME or TIMESTAMP column accepts as is. For APIs, JSON and JavaScript, send ISO 8601 with the offset: the DATE_ATOM constant or the letter c. The fixed New York time below shows what each format keeps.
<?php $now = new DateTimeImmutable(); // MySQL DATETIME: the same text SQL's NOW() returns echo $now->format('Y-m-d H:i:s'), "\n"; // ISO 8601 for APIs, JSON and JavaScript echo $now->format(DATE_ATOM), "\n"; // same as 'c' echo $now->format('Y-m-d\TH:i:s.vP'), "\n"; // with milliseconds echo $now->format(DATE_RFC2822), "\n"; // email headers // A fixed time in New York shows what each format keeps $ny = new DateTimeImmutable( '2026-07-01 08:00:00', new DateTimeZone('America/New_York'), ); echo $ny->format('Y-m-d H:i:s'), "\n"; // the offset is lost echo $ny->format('c'), "\n"; echo $ny->format(DATE_ISO8601), "\n"; // no colon in the offset $utc = $ny->setTimezone(new DateTimeZone('UTC')); echo $utc->format('Y-m-d H:i:s'), "\n"; // store this one echo json_encode(['created' => $ny->format(DATE_ATOM)]), "\n";
Output
A run on Dec 1, 2026 printed 2026-12-01 16:42:56, 2026-12-01T16:42:56+00:00, 2026-12-01T16:42:56.161+00:00 and Tue, 01 Dec 2026 16:42:56 +0000. The New York lines show why the MySQL string needs care: 2026-07-01 08:00:00 has no zone, so the reader can't tell it from 08:00 UTC. c keeps the offset as -04:00; the older DATE_ISO8601 constant writes -0400, which the PHP manual itself calls not ISO 8601 compatible, so prefer DATE_ATOM. A common rule is to store UTC (2026-07-01 12:00:00) and convert to the reader's zone on display. If the database server should stamp the row, write NOW() in the SQL instead; then the database's clock and zone apply, not PHP's.
6A clock you can freeze in tests
Code that calls time() or new DateTimeImmutable() deep inside gives a different answer every run, so a test can't check what happens at 9:30 or after a deadline. The fix is to ask for "now" from an object you pass in. The PSR-20 standard names this interface Psr\Clock\ClockInterface, with a single now(): DateTimeImmutable method, and libraries such as Symfony Clock implement it; the small version below has the same shape and no dependencies.
<?php interface Clock { public function now(): DateTimeImmutable; } final class SystemClock implements Clock { public function now(): DateTimeImmutable { return new DateTimeImmutable(); } } final class FrozenClock implements Clock { public function __construct(private DateTimeImmutable $time) { } public function now(): DateTimeImmutable { return $this->time; } } // The code asks the clock instead of reading the time itself function greeting(Clock $clock): string { $hour = (int) $clock->now()->format('G'); return match (true) { $hour < 12 => 'Good morning', $hour < 18 => 'Good afternoon', default => 'Good evening', }; } function isExpired(DateTimeImmutable $expiresAt, Clock $clock): bool { return $clock->now() >= $expiresAt; } // In production: the real time echo greeting(new SystemClock()), "\n"; // In tests: any time you like, the same answer on every run $morning = new FrozenClock(new DateTimeImmutable('2026-12-01 09:30')); $evening = new FrozenClock(new DateTimeImmutable('2026-12-01 21:15')); echo greeting($morning), "\n"; echo greeting($evening), "\n"; $expires = new DateTimeImmutable('2026-12-01 12:00'); var_dump(isExpired($expires, $morning), isExpired($expires, $evening));
Output
The first line depends on when you press Run (a 16:42 UTC run printed Good afternoon). The frozen clocks always give Good morning and Good evening, and the 12:00 deadline has not passed at 09:30 (bool(false)) but has at 21:15 (bool(true)). Because DateTimeImmutable can't be changed, handing the same frozen object to every caller is safe. For a one-off function, a plain DateTimeImmutable $now parameter does the same job.
7Which should you use?
| Method | Returns | Best for |
|---|---|---|
| date('Y-m-d H:i:s') | Formatted string | Printing now, a MySQL DATETIME value |
| date('Y') / idate('Y') | Year as string / int | Footers, age and year checks |
| new DateTimeImmutable() | Immutable object | Adding, comparing, time zones |
| new DateTime() | Mutable object | Old code; modify() changes it in place |
| time() | int seconds | Unix timestamps, expiry times |
| (int) floor(microtime(true) * 1000) | int milliseconds | Millisecond timestamps for JavaScript |
| hrtime(true) | int nanoseconds | Measuring how long code takes |
| format(DATE_ATOM) | ISO 8601 string | APIs and JSON |
Frequently asked questions
How do I get the current year in PHP?
Use date('Y'), which returns the four-digit year as a string, for example in a footer: echo 'Copyright ' . date('Y');. If you need a number to compare or calculate with, use idate('Y') or (int) date('Y'), which return an int. With an object, it is (new DateTimeImmutable())->format('Y'). All of these use the default time zone, so around New Year the year depends on it.
Is there a NOW() function in PHP like in MySQL?
No. Calling now() stops the script with Fatal error: Uncaught Error: Call to undefined function now(). Use date('Y-m-d H:i:s') or (new DateTimeImmutable())->format('Y-m-d H:i:s'), which produce the same layout as MySQL's NOW(). If you want the database server's time instead, write NOW() in the SQL query itself.
How do I get the current time in milliseconds in PHP?
Use (int) floor(microtime(true) * 1000), which gives a 13-digit integer like JavaScript's Date.now(). microtime(true) alone returns seconds as a float, and multiplying it without the cast leaves a float. (new DateTimeImmutable())->format('Uv') returns the same digits as a string. To measure elapsed time, use hrtime(true) instead, which counts nanoseconds and never jumps backwards.
Why is the time from date() off by several hours?
PHP formats the time in its default time zone, which comes from date.timezone in php.ini and is UTC when that setting is empty. Check it with date_default_timezone_get(), then set the zone you want in php.ini or with date_default_timezone_set('America/New_York') at the start of the script. For a single value, pass a zone: new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo')).
How do I fix "It is not safe to rely on the system's timezone settings"?
That warning comes from PHP 5, which raised it whenever date.timezone was not set. Set date.timezone = Europe/London (or your zone) in php.ini, or call date_default_timezone_set() before the first date function. PHP 7.0 and later no longer warn and fall back to UTC, so setting the zone is still the fix for wrong times.
Should I use DateTime or DateTimeImmutable?
Use DateTimeImmutable. Its modify(), add(), sub() and setTimezone() return a new object, while DateTime changes the object you call them on, so $end = $start->modify('+2 hours') with a DateTime moves $start too. Both classes share the same constructor and format() letters, and both implement DateTimeInterface, which is the type to accept in function parameters.
How do I get the date as dd/mm/yyyy in PHP?
Put the letters in that order: date('d/m/Y'). d is the day with a leading zero, m the month with a leading zero and Y the four-digit year; use j and n for no leading zeros, or m/d/Y for the US order. To reformat a date string you already have, parse it with DateTimeImmutable::createFromFormat() and call format() on the result.