PHP strtotime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

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.

PHP
$ts = strtotime('+1 day', 1700000000);
CallWhat 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
PHP
strtotime(string $datetime, ?int $baseTimestamp = null): int|false

Parameters

ParameterDescription
datetimeRequired. A string with a date/time format that PHP understands, such as '2023-11-14', '14 November 2023' or 'last day of next month'.
baseTimestampOptional. The timestamp that relative formats are based on. If it is null or left out, the current time is used.

Return Value

Return ValueDescription
intThe Unix timestamp for the parsed date and time.
falseReturned when the string cannot be parsed.

PHP Examples

Example 1 — Absolute and Relative Dates

Parse a normal date string, then use a relative phrase with a fixed base timestamp.

PHP
<?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;

How It Works

  1. The first call reads a full date and time and returns its timestamp.
  2. '+1 week' adds 604,800 seconds to the base timestamp.
  3. '+1 day' adds 86,400 seconds, which moves to 15 November.
  4. 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
<?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;

How It Works

  1. 'not a date' cannot be parsed, so strtotime() returns false.
  2. A string with slashes, 11/12/2023, is read as month/day/year, which is 12 November.
  3. A string with dashes, 11-12-2023, is read as day-month-year, which is 11 December.
  4. To avoid this confusion, use the ISO format Y-m-d.

Example 3 — Deadline Countdown and Next Friday

Combine strtotime() with simple math for real tasks. A fixed base keeps the output the same each time.

PHP
<?php
date_default_timezone_set('UTC');

$base = 1700000000;

$deadline = strtotime('2023-12-25 00:00:00');
$days = ceil(($deadline - $base) / 86400);

echo 'Days left: ', $days, PHP_EOL;
echo date('D, d M Y', strtotime('next friday', $base)), PHP_EOL;

How It Works

  1. The deadline is midnight on 25 December 2023.
  2. The gap from the base is just over 40 days, so ceil() rounds up to 41.
  3. 'next friday' with the base (a Tuesday) finds the following Friday, 17 November.
  4. date() formats the result as a short, readable date.

Frequently Asked Questions

It converts a date or time description in English, like '2023-11-14' or 'next Monday', into a Unix timestamp.
It returns false. Check with === false, because a valid timestamp can be 0.
Use a relative phrase, for example strtotime('+3 days', $timestamp). The second argument is the date to start from.
Slashes mean month/day/year (American style), while dashes mean day-month-year. Use Y-m-d to avoid mistakes.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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