Syntax of strtotime()
The syntax of the PHP strtotime() Function is simple:
strtotime(string $datetime, int $baseTimestamp = time()): int|false
- $datetime: This is a required parameter. It represents the date and time you want to convert into a Unix timestamp. This can be in formats like "next Monday," "+1 week," or "2025-01-24".
- $baseTimestamp: This is an optional parameter. It acts as a reference point for relative date and time calculations. By default, it is set to the current time.
The function returns an integer representing the Unix timestamp on success or false on failure.
Parameters
- $datetime (Required):
- This parameter accepts a string representing the date and time.
- Examples of valid formats:
- “2025-01-24”
- “next Tuesday”
- “+2 days”
- If the format is invalid, the function returns false.
- $baseTimestamp (Optional):
- Acts as a reference time for relative calculations.
- Default value: time() (current timestamp).
- Example:
echo strtotime("-1 day", strtotime("2025-01-01")); // Calculates one day before January 1, 2025
How Does It Works?
`strtotime` parses the input string & tries to interpret it as a date or time. It can handle a wide variety of formats, including relative dates like `"tomorrow"`, `"next week"`, or `"+2 days"`. This flexibility makes it a powerful tool for working with dates in PHP.
Example
Let’s look at a complete example to understand how `strtotime` works in real scenario:
<?php
// Example 1: Converting a simple date string to a timestamp
$dateString = "2023-10-15";
$timestamp = strtotime($dateString);
echo "Timestamp for $dateString: $timestamp\n";
// Example 2: Using a relative date string
$nextWeek = strtotime("+1 week");
echo "Timestamp for next week: $nextWeek\n";
// Example 3: Combining with date() to format the output
$formattedDate = date("Y-m-d H:i:s", strtotime("next Friday"));
echo "Next Friday will be: $formattedDate\n";
// Example 4: Handling invalid input
$invalidDate = strtotime("invalid date");
if ($invalidDate === false) {
echo "Invalid date string provided.\n";
}
?

You can also try this code with Online PHP Compiler
Run Code
Output:
Timestamp for 2023-10-15: 1697328000
Timestamp for next week: [current timestamp + 604800 seconds]
Next Friday will be: [date of next Friday]
Invalid date string provided.
In this Code
1. Example 1: We convert a simple date string (`"2023-10-15"`) into a Unix timestamp. The output is `1697328000`, which represents October 15, 2023, in seconds since the Unix epoch.
2. Example 2: We use a relative date string (`"+1 week"`) to get the timestamp for one week from now. This demonstrates how `strtotime` can handle dynamic date calculations.
3. Example 3: We combine `strtotime` with the `date` function to format the output. Here, we find the date of the next Friday & display it in a readable format (`Y-m-d H:i:s`).
4. Example 4: We test how `strtotime` handles invalid input. If the input string cannot be parsed, the function returns `false`.
Why Use strtotime?
- Flexibility: It can parse a wide range of date formats, including relative dates.
- Ease of Use: You don’t need to manually calculate timestamps or handle complex date logic.
- Integration: It works seamlessly with other PHP date functions like `date()` & `mktime()`.
Example
Here are some examples to demonstrate the usage of strtotime():
Example 1: Converting a String Date to a Timestamp
<?php
$dateString = "2025-01-24";
$timestamp = strtotime($dateString);
if ($timestamp !== false) {
echo "The Unix timestamp for $dateString is: $timestamp";
} else {
echo "Invalid date format.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The Unix timestamp for 2025-01-24 is: 1737686400
This example shows how strtotime() converts a specific date string into a Unix timestamp.
Example 2: Relative Dates
<?php
$relativeDate = "+1 week";
$timestamp = strtotime($relativeDate);
if ($timestamp !== false) {
echo "The Unix timestamp for '$relativeDate' from now is: $timestamp";
echo "\nThis corresponds to: " . date("Y-m-d H:i:s", $timestamp);
} else {
echo "Invalid relative date format.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The Unix timestamp for '+1 week' from now is: 1738291200
This corresponds to: 2025-02-01 00:00:00
This demonstrates how strtotime() handles relative date strings and converts them into Unix timestamps.
Example 3: Using the Base Timestamp
<?php
$baseDate = "2025-01-24";
$relativeDate = "+2 days";
$baseTimestamp = strtotime($baseDate);
$newTimestamp = strtotime($relativeDate, $baseTimestamp);
if ($newTimestamp !== false) {
echo "The new date after $relativeDate from $baseDate is: " . date("Y-m-d", $newTimestamp);
} else {
echo "Error in date conversion.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The new date after +2 days from 2025-01-24 is: 2025-01-26
Here, the strtotime() function calculates a future date using a specific base timestamp.
Advanced Usage: Using strtotime() with Time Zones
Handling time zones can be crucial in applications requiring global date and time calculations. strtotime() works seamlessly with PHP’s date_default_timezone_set() function.
Example: Changing the Time Zone
<?php
// Set a specific time zone
date_default_timezone_set("America/New_York");
$newYorkTime = strtotime("2025-01-24 10:00:00");
// Switch to another time zone
date_default_timezone_set("Asia/Kolkata");
$kolkataTime = strtotime("2025-01-24 10:00:00");
if ($newYorkTime && $kolkataTime) {
echo "New York time (timestamp): $newYorkTime\n";
echo "Converted to: " . date("Y-m-d H:i:s", $newYorkTime) . "\n";
echo "Kolkata time (timestamp): $kolkataTime\n";
echo "Converted to: " . date("Y-m-d H:i:s", $kolkataTime);
} else {
echo "Error in converting dates.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
New York time (timestamp): 1737732000
Converted to: 2025-01-24 10:00:00
Kolkata time (timestamp): 1737686400
Converted to: 2025-01-24 10:00:00
This example highlights how timestamps can vary depending on the time zone settings.
Frequently Asked Questions
What happens if strtotime() receives an invalid date string?
If strtotime() cannot interpret the provided string, it returns false. Always validate the output before using it.
Can PHP strtotime() Function handle different date formats?
Yes, strtotime() supports a wide range of date formats, such as "Y-m-d," "d/m/Y," and relative formats like "+2 weeks."
Does strtotime() account for daylight saving time (DST)?
Yes, strtotime() considers DST based on the current time zone setting in PHP. Ensure you set the correct time zone using date_default_timezone_set().
Conclusion
The PHP strtotime() Function is an important tool for PHP developers, enabling effortless conversion of textual date descriptions into Unix timestamps. By understanding its syntax, parameters, and advanced features like time zone handling, developers can leverage this function for efficient date and time management.