English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP strtotime() Function Usage and Example

PHP Date & Time Function Manual

The strtotime() function parses any string description of date and time into Unix timestamp

Definition and usage

strtotime()Function function accepts text dates with/Date of the time value/Time string, and parse it into Unix timestamp.

Note: If the year is represented using a two-digit format, the value 0-69 will map to 2000-2069value 70-100 will map to 1970-2000.
Note: Please note m/d/y or d-m-y formatted date, if the separator is a slash (/),then use the American m/d/y format. If the separator is a hyphen (-) or a point (.), use the European d-m-y format. To avoid potential errors, you should use YYYY as much as possible-MM-DD format or use the date_create_from_format() function.

Syntax

strtotime($time)

Parameter

Serial numberParameters and descriptions
1

time (required)

This value represents the date/Time string.

2

now (optional)

This indicates a timestamp used as the basis for calculating relative dates.

Return value

The PHP strtotime() function returns the timestamp value of the given date string. If it fails, this function will return a boolean valuefalse.

PHP version

This function was originally in PHP 4Introduced in version .0 and can be used in all higher versions.
PHP 5.3.0: The relative time format, such as this week, last week, the previous week, the next week, specifies a week from Monday to Sunday, rather than using a relative date to the current date/Time before and after 7 days.
PHP 5.3.0: Now 24:00 is a valid format.
PHP 5.2.7: In earlier versions, if you requested a specific date in a month and that date was the first day of the month, it would incorrectly add a week to the returned timestamp, which has now been corrected.
PHP 5.1.0: If it fails, it returns FALSE (in earlier versions it returned -1): Added E_STRICT and E_NOTICE timezone errors.
PHP 5.0.2: Now correctly calculates "now" and other relative times based on the current time, not based on midnight of today.
PHP 5.0.0: Allows microseconds (but microsecond numbers are usually ignored).

Online Example

The following examples demonstratestrtotime()Usage of the function-

<?php
   $str = strtotime("12 September 2009");
   print("Timestamp: ",$str); 
?>
Test and see‹/›

Output Result

Timestamp: 1252713600

Online Example

If passed“ now”If used as a parameter, this function returns the current timestamp

<?php
   $str = strtotime("now");
   print("Timestamp: ",$str); 
?>
Test and see‹/›

Output Result

Timestamp: 1589369948

Online Example

Now, let's call this method by passing various date values-

<?php
// Set Time Zone
date_default_timezone_set("PRC");
$time = strtotime("2020-01-18 08:08:08");  // Convert a specified date to a timestamp 
// Print the current time PHP_EOL newline character, compatible with different systems
echo $time, PHP_EOL;
 
// More Examples
echo strtotime("now"), PHP_EOL;
echo strtotime("now"), PHP_EOL;
echo strtotime("10 September 2019"), PHP_EOL;
echo strtotime("+1 day"), PHP_EOL;
echo strtotime("+1 week"), PHP_EOL;
echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL;
echo strtotime("next Thursday"), PHP_EOL;
echo strtotime("last Monday"), PHP_EOL;
?>
Test and see‹/›

This will produce the following output-

1579306088
1596165501
1596165501
1568044800
1596251901
1596770301
1596957503
1596643200
1595779200

Online Example

formatted as YYYY-MM-DD, and output the date

<?php
   $timestamp = strtotime("February 15, 2015" );   
   print date('Y-m-d', $timestamp );
?>
Test and see‹/›

This will produce the following output-

2015-02-15