Member-only story
The strtotime() function is a built-in PHP function that allows you to parse a string that represents a date and time, and returns the Unix timestamp representing that date and time. The Unix timestamp is a long integer value that represents the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC.
Here’s a quick tutorial on how to use the strtotime() function in PHP:
Basic usage
The most basic use of the strtotime() function is to pass it a date string and have it return the Unix timestamp. Here’s an example:
$date_str = '2022-02-22';
$timestamp = strtotime($date_str);
echo $timestamp;
In this example, we pass the date string ‘2022–02–22’ to the strtotime() function, which returns the Unix timestamp for that date (i.e., the number of seconds since January 1, 1970). We store the timestamp in a variable $timestamp
and then print it using the echo statement.
Date and time formats
The strtotime() function can recognize a wide variety of date and time formats, including the ISO 8601 format, which is the default format for dates in PHP. Here are some examples of valid date and time formats:
- ‘2022–02–22’ — A date in the ISO 8601 format
- ‘2022–02–22 14:30:00’ — A date and time in the ISO 8601 format
- ‘2022–02–22T14:30:00Z’ — A date and time in the ISO 8601 format with a…