Register Login

PHP strtotime() function

Updated Jan 08, 2020

What PHP Strtotime() Function?

PHP strtotime() function is an in-built PHP function which is used to convert the English textual DateTime into Unix timesmap datetime (number of seconds since January 1 1970 00:00:00 GMT). 

Important Notes: 

  • Each parameter of strtotime()  by default takes default time zone unless the specific time zone is specified.
  • If in case the user specifies the year in 2 digit format than values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
  • Also, users have to be careful with date separator while using this function; for slash (/) separator American (m/d/y) is assumed, for the dash (-) or dot (.) European (d-m-y) format is assumed. Therefore to avoid any confusion, you should use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

What is Unix Timesmap?

Unix timesmap is the total number of seconds completed since January 1 1970 00:00:00 GMT.

Syntax:

strtotime ( string $time, int $now)

Parameters

The strtotime() function takes two parameters as shown above in the syntax.

Parameter Type Description
$time (mandatory) string specifies the English textual date/time string
$now (optional) integer specifies the timestamp which is used as a base to calculate relative dates

Return Value

The strtotime() returns timestamp on success and FALSE on failure.

Note: PHP versions previous to PHP 5.1.0, would return -1 on failure.

PHP strtotime() Example

Note: The output of below programs may vary depending on the date/time the code is compiled

Program 1: 

<?php
#PHP program to illustrate the working of strtotime Function

#simple Example

//Variable to hold the returned value of strtotime Function

/*
 NOTE : This function returns the UNIX timestamp
*/

$dateTimeNow = strtotime("now");

//Printing the variable value

echo "Current Date Time : ".$dateTimeNow;
?> 

Output

Current Date Time : 1573022861 

Program 2: 

PHP program to convert UNIX timestamp into date format

<?php
#PHP program to illustrate the working of strtotime Function

#simple Example (Convert UNIX timestamp into date format)

//Variable to hold the returned value of strtotime Function

$dateTimeNow = strtotime("now");

$dateTimeNowInDateFormat = date("Y-m-d h:i:s", $dateTimeNow);

//Printing the variable value

echo "Current Date Time : ".$dateTimeNowInDateFormat;
?> 

Output:

Current Date Time : 2019-11-06 07:04:15 

Program 3

Convert English Date to UNIX timestamp

<?php
#PHP program to illustrate the working of strtotime Function

#Convert English Date to UNIX timestamp

//Variable to hold the returned value of strtotime Function

$dateType_1 = strtotime("now");
$dateType_2 = strtotime("03 November 2019");
$dateType_3 = strtotime("+12 hours");
$dateType_4 = strtotime("-12 hours");
$dateType_5 = strtotime("+2 weeks");
$dateType_6 = strtotime("-2 weeks");
$dateType_7 = strtotime("+2 weeks 4 days 5 hours 50 seconds");
$dateType_8 = strtotime("next saturday");
$dateType_9 = strtotime("last saturday");
$dateType_10 = strtotime("+1 month");
$dateType_11 = strtotime("-1 month");

//Printing the variable value

echo "Current Date Time : ".$dateType_1;
echo "<br>";
echo "Current Date Time : ".$dateType_2;
echo "<br>";
echo "Current Date Time : ".$dateType_3;
echo "<br>";
echo "Current Date Time : ".$dateType_4;
echo "<br>";
echo "Current Date Time : ".$dateType_5;
echo "<br>";
echo "Current Date Time : ".$dateType_6;
echo "<br>";
echo "Current Date Time : ".$dateType_7;
echo "<br>";
echo "Current Date Time : ".$dateType_8;
echo "<br>";
echo "Current Date Time : ".$dateType_9;
echo "<br>";
echo "Current Date Time : ".$dateType_10;
echo "<br>";
echo "Current Date Time : ".$dateType_11;

?> 

Output

Current Date Time : 2019-11-06 08:02:52
Current Date Time : 2019-11-03 12:00:00
Current Date Time : 2019-11-06 08:02:52
Current Date Time : 2019-11-05 08:02:52
Current Date Time : 2019-11-20 08:02:52
Current Date Time : 2019-10-23 08:02:52
Current Date Time : 2019-11-24 01:03:42
Current Date Time : 2019-11-09 12:00:00
Current Date Time : 2019-11-02 12:00:00
Current Date Time : 2019-12-06 08:02:52
Current Date Time : 2019-10-06 08:02:52

Program 4:

PHP program to convert English Date to UNIX timestamp then normal date

<?php
#PHP program to illustrate the working of strtotime Function

#Convert English Date to UNIX timestamp then normal Date

//Variable to hold the returned value of strtotime Function

$dateType_1 = strtotime("now");
$dateType_2 = strtotime("03 November 2019");
$dateType_3 = strtotime("+12 hours");
$dateType_4 = strtotime("-12 hours");
$dateType_5 = strtotime("+2 weeks");
$dateType_6 = strtotime("-2 weeks");
$dateType_7 = strtotime("+2 weeks 4 days 5 hours 50 seconds");
$dateType_8 = strtotime("next saturday");
$dateType_9 = strtotime("last saturday");
$dateType_10 = strtotime("+1 month");
$dateType_11 = strtotime("-1 month");

//Printing the variable value

echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_1);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_2);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_3);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_4);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_5);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_6);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_7);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_8);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_9);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_10);
echo "<br>";
echo "Current Date Time : ".date("Y-m-d h:i:s",$dateType_11);

?> 

Output

Current Date Time : 2019-11-06 09:05:48
Current Date Time : 2019-11-03 12:00:00
Current Date Time : 2019-11-06 09:05:48
Current Date Time : 2019-11-05 09:05:48
Current Date Time : 2019-11-20 09:05:48
Current Date Time : 2019-10-23 09:05:48
Current Date Time : 2019-11-24 02:06:38
Current Date Time : 2019-11-09 12:00:00
Current Date Time : 2019-11-02 12:00:00
Current Date Time : 2019-12-06 09:05:48
Current Date Time : 2019-10-06 09:05:48

 


×