strtotime

Parses an English date or time description into a Unix timestamp.

Signature
strtotime(string $datetime, ?int $baseTimestamp = null): int|false
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$datetimestringA date/time string in one of the supported English formats, absolute or relative.
$baseTimestampoptional?int= nullThe timestamp relative dates are calculated from. Defaults to the current time when omitted or null.

Return value

int|falseA Unix timestamp on success, or false if the string could not be parsed.

Errors

Emits E_WARNING if the default time zone is not valid. Returns false rather than throwing when the string cannot be parsed.

Changelog

  • PHP 8.0.0baseTimestamp is nullable now.

strtotime() reads an English date expression and returns a timestamp. It handles absolute dates (2025-06-15), times, time zones, and — its real strength — relative expressions like +3 days, last friday or first day of next month.

When to reach for it

Use it to normalise dates arriving as text (log lines, CSV columns, API strings), and for quick relative arithmetic: token expiry with strtotime('+1 hour'), a report range with strtotime('first day of last month'). Passing $baseTimestamp makes relative parsing deterministic, which is what you want in tests.

What trips people up

Separator, not order, decides the ambiguous case. 10/09/2025 with slashes is read as American m/d/y (9 October); 10-09-2025 with dashes or dots is read as European d-m-y (10 September). If the source format is known, parse it explicitly with DateTimeImmutable::createFromFormat() instead of guessing.

Failure returns false, and false loosely equals 0 — which is a valid timestamp (1 January 1970). Always test with === false.

The result is a plain integer with no time zone attached. The string is interpreted in the default time zone unless it carries its own offset, so a script that changes date_default_timezone_set() mid-run gets different answers for the same input.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Normalise dates from a CSV import or a log file into timestamps for sorting
  • Set a token or cache expiry with strtotime('+30 minutes')
  • Compute the reporting window for last month with 'first day of last month' and 'last day of last month'

Frequently asked questions

Why does strtotime return false?

The string did not match any supported English date format. Check the separator style and the language, and remember that non-English month names are never understood.

Does strtotime validate dates?

No. Impossible dates such as 2025-02-30 are rolled over into the next month rather than rejected, so use checkdate for validation.

What is the difference between strtotime and DateTimeImmutable::createFromFormat?

strtotime guesses the format from the string, while createFromFormat requires you to state it. Use createFromFormat when the input format is known and fixed, since guessing is what causes day/month swaps.

More examples

Slashes versus dashes

The same three digits produce 2025-10-09 with slashes and 2025-09-10 with dashes or dots. createFromFormat removes the guesswork entirely, which is what import code should use.

Example · php
<?php
date_default_timezone_set('UTC');

echo 'slashes: ', date('Y-m-d', strtotime('10/09/2025')), "\n"; // m/d/y
echo 'dashes:  ', date('Y-m-d', strtotime('10-09-2025')), "\n"; // d-m-y
echo 'dots:    ', date('Y-m-d', strtotime('10.09.2025')), "\n"; // d.m.y

// unambiguous when you say so explicitly
$d = DateTimeImmutable::createFromFormat('d/m/Y', '10/09/2025');
echo 'explicit: ', $d->format('Y-m-d'), "\n";

Safely validating user input

Only the empty string and 'garbage' fail. Note that '2025-02-30' does not fail: it rolls over to 2 March, so strtotime is not a date validator. The epoch line shows why === false matters, since its timestamp is 0.

Example · php
<?php
date_default_timezone_set('UTC');

foreach (['2025-02-30', 'tomorrow', '', 'garbage', '1970-01-01 UTC'] as $input) {
    $ts = strtotime($input);
    if ($ts === false) {
        echo "'$input' => could not parse\n";
    } else {
        echo "'$input' => ", date('Y-m-d', $ts), "\n";
    }
}

Discussion

  • Be the first to comment on this lesson.