PHP strtotime() Function

Learn via video courses
Topics Covered

Overview

strtotime is a powerful function in PHP that converts human-readable date and time expressions into Unix timestamps. This function accommodates a wide range of formats, allowing developers to parse dates and times in various styles.

It's particularly useful for tasks like calculating intervals, formatting dates, and handling dynamic time-based operations.

Syntax of strtotime() in PHP

Here is the detailed syntax of strtotime() in PHP:

  • time: A string representing the date and/or time expression you want to parse.
  • now (optional): A Unix timestamp representing the reference time. If not provided, the current timestamp is used as the reference.

Parameter Values of strtotime() in PHP

The strtotime() function in PHP accepts two parameters: the time string to be parsed and an optional reference timestamp. Here are the details of each parameter:

  1. time (Required):
  • This parameter is the string representing the date and/or time expression you want to parse and convert into a Unix timestamp.
  • It can be a wide variety of formats and expressions, including relative phrases, specific dates, times, calculations, and more.
  • The time parameter is case-insensitive, so you can use upper or lower case letters for the expressions.
  1. now (Optional):
  • This parameter is an optional Unix timestamp representing the reference time. It defines the base point against which the relative expressions in the time parameter are calculated.
  • If the new parameter is not provided, the current timestamp (obtained using the time() function) is used as the reference time.
  • You can provide your own Unix timestamp to customize the reference time for parsing relative expressions.

Return Value of strtotime() in PHP

The strtotime() function in PHP returns a Unix timestamp corresponding to the parsed date and time expression.

However, depending on the input and the success of the parsing, it can also return other values. Here are the details of the possible return values:

  1. Unix Timestamp (Success):
  • If the parsing of the provided time string is successful, strtotime() returns a Unix timestamp. This timestamp represents the number of seconds since the Unix epoch (January 1, 1970, UTC).
  • The returned timestamp corresponds to the parsed date and time expression, considering any relative or absolute information provided in the input.
  1. False (Failure):
  • If the parsing of the time string fails, strtotime() returns false.
  • Parsing might fail if the input is not recognized as a valid date or time expression, or if it contains ambiguous or conflicting information.
  1. Warning (Failure):
  • In some cases, where the input is recognized as a valid date but it falls outside the representable range of Unix timestamps (before 1970 or after 2038 on 32-bit systems), strtotime() returns a Unix timestamp value of -1.
  • Additionally, if the input is not recognized as a valid expression but doesn't cause a parsing failure, strtotime() may return a Unix timestamp value of -1.

Examples of strtotime() in PHP

Program to demonstrate the strtotime()

Here's an example of using the strtotime() function in PHP, followed by an explanation:

In this example, the program demonstrates how to use the strtotime() function to calculate a future date. The code calculates the timestamp for the date which is exactly one week from the current moment. The strtotime() function takes the relative expression "+1 week" as its parameter, which signifies adding one week to the current date and time. The resulting timestamp is then formatted using the date() function with the format "Y-m-d" to display the future date in the "Year-Month-Day" format. The program then echoes the calculated future date, showing the user-friendly representation of the date one week from the present moment.

When the English text is a date

In PHP, the strtotime() function is capable of recognizing certain English textual time descriptions and converting them into Unix timestamps. Here's an example of using strtotime() when the English text represents a date, followed by an explanation:

In this example, the program showcases how strtotime() can be used to parse English textual time descriptions that represent dates. The variable $englishDate holds the value "next Tuesday", which represents the upcoming Tuesday from the current date and time. The strtotime() function processes this English text and calculates the corresponding Unix timestamp. The resulting timestamp represents the exact moment of the next Tuesday.

When the English text corresponds to any day

In PHP, the strtotime() function is capable of interpreting English textual descriptions that correspond to specific days of the week. These descriptions are used to calculate dates based on the provided information. Let's look at an example along with an explanation:

In this example, the program showcases the use of strtotime() to find the date of the next occurrence of a specific day of the week. The function takes the relative expression "next Monday" as its parameter, indicating that we want to find the next Monday from the current date.

If you pass "now" as a parameter

When you pass the string "now" as a parameter to the strtotime() function in PHP, it represents the current date and time. Here's an example and an explanation:

In this example, the code utilizes the strtotime() function with the string "now" as its parameter. When "now" is used, it signifies the current date and time at the moment when the function is executed. The strtotime() function interprets "now" and returns the Unix timestamp representing the current date and time. The resulting timestamp is then formatted using the date() function with the format "Y-m-d H:i", which displays the date and time in the "Year-Month-Day Hour:Minute" format.

Conclusion

  • Flexible Date Parsing: The strtotime() function provides a flexible and convenient way to parse human-readable date and time expressions into Unix timestamps. It simplifies working with dates and times by allowing developers to use natural language phrases, relative expressions, and specific date formats.
  • Relative Calculations: strtotime() excels in calculating relative dates and times, such as "tomorrow," "next week," or "3 days ago." It enables dynamic time-based operations by easily adding or subtracting intervals from a reference time.
  • Unix Timestamps: The primary return value of strtotime() is a Unix timestamp, representing the number of seconds since the Unix epoch. This format is widely used for date and time calculations in programming.
  • Edge Cases and Limitations: While powerful, strtotime() has limitations. It might return false or -1 in cases of parsing failure or when the result falls outside the representable timestamp range.