PHP strtotime() Function

In this chapter you will learn:

  1. Definition for PHP strtotime() Function
  2. Syntax for PHP strtotime() Function
  3. Parameter for PHP strtotime() Function
  4. Return for PHP strtotime() Function
  5. Example - Use PHP strtotime() Function

Definition

The strtotime() function converts strings to a timestamp

Syntax

PHP strtotime() Function has the following syntax.

int strtotime ( string time [, int now] )

Parameter

time is the the string time to convert, a second optional parameter that can be a relative timestamp.

Return

Parse about any English textual datetime description into a Unix timestamp

Example

Consider this script:


<?PHP/*from j  ava  2 s  .c  om*/
print strtotime("22nd December 2012"); 
print strtotime("22 Dec. 1979 17:30"); 
print strtotime("2012/12/22"); 
?>

The integer value are the Unix timestamps for the dates we passed into strtotime(). You must use American-style dates (i.e., month, day, year) with strtotime().

If PHP is unable to convert your string into a timestamp, it will return -1.


<?PHP//from   ja va2s.c o  m
$mydate = strtotime("Christmas 2012"); 
if ($mydate == -1) { 
   print "Date conversion failed!"; 
} else { 
   print "Date conversion succeeded!"; 
} 
?>

The optional second parameter sets a timestamp to for relative dates. The date string in the first parameter can be relative dates such as "Next Sunday," "20 days," or "7 year ago." In this situation, the second parameter sets the relative times.

For example, this next line of code will print the timestamp for the next Sunday:


<?PHP
print strtotime("Next Sunday"); 
?>

"2 days" after the current timestamp.


<?PHP
print strtotime("2 days", time()); 
?>

subtracts a year from a given timestamp.


<?PHP
print strtotime("1 year ago", 123456789); 
?>

"August 25, 2012, 11:26 p.m." is not a legal string because of the comma.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP time() Function
  2. Syntax for PHP time() Function
  3. Example - gets the current time in epoch format
Home » PHP Tutorial » PHP Date Functions
PHP checkdate() Function
PHP date() Function
PHP date_add() Function
PHP date_create() Function
PHP date_create_from_format() Function
PHP date_date_set() Function
PHP date_default_timezone_get() Function
PHP date_default_timezone_set() Function
PHP date_diff() Function
PHP date_format() Function
PHP date_get_last_errors() Function
PHP date_interval_format() Function
PHP date_isodate_set() Function
PHP date_modify() Function
PHP date_offset_get() Function
PHP date_parse() Function
PHP date_parse_from_format() Function
PHP date_sub() Function
PHP date_sun_info() Function
PHP date_sunrise() Function
PHP date_sunset() Function
PHP date_time_set() Function
PHP date_timestamp_get() Function
PHP date_timestamp_set() Function
PHP date_timezone_get() Function
PHP date_timezone_set() Function
PHP getdate() Function
PHP gettimeofday() Function
PHP gmdate() Function
PHP gmmktime() Function
PHP gmstrftime() Function
PHP idate() Function
PHP localtime() Function
PHP microtime() Function
PHP mktime() Function
PHP strftime() Function
PHP strptime() Function
PHP strtotime() Function
PHP time() Function
PHP Timezones Constant
PHP timezone_abbreviations_list() Function
PHP timezone_identifiers_list() Function
PHP timezone_location_get() Function
PHP timezone_name_from_abbr() Function
PHP timezone_name_get() Function
PHP timezone_offset_get() Function
PHP timezone_open() Function
PHP timezone_version_get() Function