strtotime() Since: PHP 4(2000)
Parses an English-formatted date/time string and converts it to a Unix timestamp. It can also interpret relative expressions such as '+1 week' or 'next Monday'.
Syntax
// Converts a date/time string to a Unix timestamp. strtotime($datetime, $baseTimestamp);
Parameters
| Parameter | Description |
|---|---|
| $datetime | The date/time string to parse. You can specify an absolute date such as "2025-04-15" or a relative expression such as "+1 day". |
| $baseTimestamp | The base timestamp for relative date calculations. If omitted, the current time is used. |
Return Value
Returns the Unix timestamp as an integer on success. Returns false if parsing fails.
Sample Code
<?php
// Converts a date string to a timestamp.
$ts = strtotime("2025-04-15");
echo date("Y-m-d", $ts); // Outputs '2025-04-15'.
// Various date string formats can be parsed.
echo date("Y-m-d", strtotime("April 15, 2025")); // Outputs '2025-04-15'.
echo date("Y-m-d", strtotime("15 Apr 2025")); // Outputs '2025-04-15'.
// Using relative date/time expressions.
echo date("Y-m-d", strtotime("+1 day")); // Outputs tomorrow's date.
echo date("Y-m-d", strtotime("-1 week")); // Outputs the date one week ago.
echo date("Y-m-d", strtotime("+3 months")); // Outputs the date three months from now.
echo date("Y-m-d", strtotime("+1 year")); // Outputs the date one year from now.
// Day-of-week expressions are also supported.
echo date("Y-m-d", strtotime("next Monday")); // Outputs the date of next Monday.
echo date("Y-m-d", strtotime("last Friday")); // Outputs the date of last Friday.
// Compound expressions can also be parsed.
echo date("Y-m-d", strtotime("first day of next month")); // Outputs the first day of next month.
echo date("Y-m-d", strtotime("last day of this month")); // Outputs the last day of this month.
// Calculates a relative date from a base timestamp.
$base = strtotime("2025-01-01");
echo date("Y-m-d", strtotime("+90 days", $base)); // Outputs '2025-04-01'.
// Can be used to check expiration dates.
$expire_date = "2025-12-31";
if (strtotime($expire_date) > time()) {
echo "The date is still valid.";
} else {
echo "The date has expired.";
}
// Handling a failed parse.
$result = strtotime("invalid date string");
var_dump($result); // Outputs 'bool(false)'.
Notes
strtotime() converts an English date/time string into a Unix timestamp. In addition to ISO 8601 format, it can parse natural-language-like English expressions such as "next Monday" and "last day of this month", making it widely used for date arithmetic and scheduling.
Be careful with '+1 month'. Applying '+1 month' to January 31 results in March 3, because February 31 does not exist. For end-of-month calculations, use "last day of next month" or control the date explicitly with the DateTime class.
Because the return value can be false, you should always validate the result after parsing. Use date() to format the output. For more advanced date/time manipulation, the object-oriented DateTime class is recommended.
If you find any errors or copyright issues, please contact us.