PHP strtotime() Function with Examples

The strtotime() function in PHP is a built-in function that converts a human-readable date and time string into a Unix timestamp.

A Unix timestamp represents the number of seconds since January 1, 1970 00:00:00 GMT. You can use this function to perform date and time operations such as adding or subtracting intervals from a given date.

Syntax of strtotime() Function in PHP


The basic syntax of strtotime() function in PHP is as:

strtotime(datetime, baseTimestamp)

Parameters:

PHP strtotime() function accepts two parameters:

  • datetime (Required): This parameter specifies the date/time string. Example: “now”, “10 September 2000”, “next Monday”, “last Friday”, “tomorrow”, etc.
  • baseTimestamp (Optional): This parameter specifies the timestamp used as the base for calculating relative dates. If omitted, the current time is used.

Return Value:

  • Returns a Unix timestamp (integer) on success.
  • Returns false if the date/time string is invalid.

Basic Examples of PHP strtotime() Function


Let’s take some important examples based on PHP strtotime() function.

Example 1: Tomorrow

You can use natural English phrases like “tomorrow”, “next week”, “next Sunday”, “last Friday”, etc.

<?php
echo date("Y-m-d", strtotime("tomorrow"));
?>

Output:

2025-10-14

In this example, we have used the strtotime() function that converts a human-readable date/time string into a Unix timestamp. Here, the string “tomorrow” is interpreted as 24 hours after the current time. Therefore, the strtotime(“tomorrow”) function returns the Unix timestamp for tomorrow’s date at 00:00:00 (midnight). For example, if today is October 13, 2025, then strtotime(“tomorrow”) returns the timestamp for October 14, 2025, 00:00:00.


Then, we have called the date() function to format a given timestamp into a human-readable string. The format “Y-m-d” means:

  • Y → 4-digit year (e.g., 2025)
  • m → 2-digit month (e.g., 10)
  • d → 2-digit day (e.g., 14)

So, it converts the timestamp for tomorrow into a date string like “2025-10-14”.

Example 2: Next week

<?php
echo date("Y-m-d", strtotime("next week"));
?>

Output:

2025-10-20

Example 3: Specific date

<?php
$timestamp = strtotime("2025-10-08");
echo date("l, F j, Y", $timestamp);
?>

Output:

Wednesday, October 8, 2025

These examples dynamically calculate dates based on the current day.

Example 4: Convert Date String to Timestamp in PHP

<?php
$dateString = "08 October 2025";
$timestamp = strtotime($dateString);
echo "Timestamp for $dateString is: " . $timestamp;
?>

Output:

Timestamp for 08 October 2025 is: 1759874400

In this example, the strtotime() function converts the string “08 October 2025” is converted into a Unix timestamp.

Example 5: Adding or Subtracting Days

PHP strtotime() function allows arithmetic operations like adding or subtracting days, months, or years. Look at the below example.

<?php
$today = date("Y-m-d");
$nextWeek = date("Y-m-d", strtotime("+1 week"));
$lastMonth = date("Y-m-d", strtotime("-1 month"));
$nextYear = date("Y-m-d", strtotime("+1 year"));

echo "Today: $today <br>";
echo "Next Week: $nextWeek <br>";
echo "Last Month: $lastMonth <br>";
echo "Next Year: $nextYear";
?>

Output:

Today: 2025-10-13
Next Week: 2025-10-20
Last Month: 2025-09-13
Next Year: 2026-10-13

Example 6: Using strtotime() Function with Base Timestamp

The second parameter (baseTimestamp) in the strtotime() function lets you calculate dates relative to a specific time rather than current time.

<?php
$baseDate = strtotime("2025-01-01");
$newDate = strtotime("+10 days", $baseDate);
echo "10 days after 2025-01-01 is: " . date("Y-m-d", $newDate);
?>

Output:

10 days after 2025-01-01 is: 2025-01-11

Example 7: Find Difference Between Two Dates

You can use strtotime() function to calculate date differences in days.

<?php
$date1 = strtotime("2025-10-13");
$date2 = strtotime("2025-12-31");
$diff = ($date2 - $date1) / (60 * 60 * 24);
echo "Days between dates: " . $diff;
?>

Output:

Days between dates: 79

Example 8: Calculate Future Expiry Date (Advanced Example)

Suppose you have a subscription system where each user’s plan expires after 30 days. You can use strtotime() function to calculate the expiry date.

<?php
$signupDate = "2025-10-13";
$expiryTimestamp = strtotime("+30 days", strtotime($signupDate));
$expiryDate = date("Y-m-d", $expiryTimestamp);

echo "Signup Date: $signupDate <br>";
echo "Expiry Date: $expiryDate";
?>

Output:

Signup Date: 2025-10-13
Expiry Date: 2025-11-12

Conclusion

In this tutorial, you learned how to use the PHP strtotime() function to convert human-readable date and time strings into Unix timestamps. I hope that you will have understood the basic syntax of strtotime() function and practiced all basic to advanced examples.