Home PHP Date/Time strtotime() PHP strtotime() Function Definition In PHP, strtotime() converts an English date and time description, such as '2023-11-14' or 'next Monday', into a Unix timestamp.
It is very flexible. You can pass a normal date, a relative phrase like '+1 week', or words like 'tomorrow'. The optional second argument sets the starting point for relative phrases.
Syntax Call strtotime() with the values you need. Start with this line.
$ts = strtotime('+1 day', 1700000000); Call What it does strtotime('2023-11-14 22:13:20')Returns 1700000000 when the time zone is UTC. strtotime('+1 day', 1700000000)Returns 1700086400, exactly 24 hours later. strtotime('next monday', 1700000000)Returns 1700438400, midnight on 20 November 2023 in UTC.
Pass a base timestamp whenever you use relative words like 'tomorrow' or '+1 week', so the result is predictable. Without it, PHP uses the current time. For strict formats, DateTimeImmutable::createFromFormat() is safer than strtotime().
Library declarations strtotime(string $datetime, ?int $baseTimestamp = null): int|false Parameters Parameter Description datetime Required. A string with a date/time format that PHP understands, such as '2023-11-14', '14 November 2023' or 'last day of next month'. baseTimestamp Optional. The timestamp that relative formats are based on. If it is null or left out, the current time is used.
Return Value Return Value Description int The Unix timestamp for the parsed date and time. false Returned when the string cannot be parsed.
Important
Always compare the result with === false. A valid timestamp can be 0, which also looks 'empty' to a loose check. Also, 11/12/2023 is read as month/day (American) while 11-12-2023 is read as day-month.
PHP Examples 1. Absolute and Relative Dates 2. Invalid Text and Ambiguous Formats 3. Deadline Countdown and Next Friday Example 1 — Absolute and Relative Dates Parse a normal date string, then use a relative phrase with a fixed base timestamp.
<?php
date_default_timezone_set('UTC');
$base = 1700000000;
echo strtotime('2023-11-14 22:13:20'), PHP_EOL;
echo strtotime('+1 week', $base), PHP_EOL;
echo date('Y-m-d', strtotime('+1 day', $base)), PHP_EOL; 1700000000
1700604800
2023-11-15 How It Works The first call reads a full date and time and returns its timestamp. '+1 week' adds 604,800 seconds to the base timestamp. '+1 day' adds 86,400 seconds, which moves to 15 November. date() turns that timestamp back into a readable date. Example 2 — Invalid Text and Ambiguous Formats strtotime() returns false for text it cannot read, and the separator changes how numeric dates are understood.
<?php
date_default_timezone_set('UTC');
echo strtotime('not a date') === false ? 'Invalid date string' : 'Parsed', PHP_EOL;
echo date('Y-m-d', strtotime('11/12/2023')), PHP_EOL;
echo date('Y-m-d', strtotime('11-12-2023')), PHP_EOL; Invalid date string
2023-11-12
2023-12-11 How It Works 'not a date' cannot be parsed, so strtotime() returns false. A string with slashes, 11/12/2023, is read as month/day/year, which is 12 November. A string with dashes, 11-12-2023, is read as day-month-year, which is 11 December. To avoid this confusion, use the ISO format Y-m-d. Frequently Asked Questions What does strtotime() do in PHP? It converts a date or time description in English, like '2023-11-14' or 'next Monday', into a Unix timestamp.
What does strtotime() return on failure? It returns false. Check with === false, because a valid timestamp can be 0.
How do I add days to a date with strtotime()? Use a relative phrase, for example strtotime('+3 days', $timestamp). The second argument is the date to start from.
Why does strtotime('11/12/2023') give a different date than strtotime('11-12-2023')? Slashes mean month/day/year (American style), while dashes mean day-month-year. Use Y-m-d to avoid mistakes.
Should I use strtotime() or DateTime? strtotime() is quick for simple tasks. For time zones, exact formats and date math, DateTimeImmutable is easier to reason about.
Next: time() time() returns the current time as a Unix timestamp. That is the number of seconds that ha.
time() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion