<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Parse about any English textual datetime description into a Unix timestamp</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strptime.html">strptime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.time.html">time</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.strtotime" class="refentry"> <div class="refnamediv"> <h1 class="refname">strtotime</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strtotime</span> — <span class="dc-title">Parse about any English textual datetime description into a Unix timestamp</span></p> </div> <div class="refsect1 description" id="refsect1-function.strtotime-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>strtotime</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$now</code><span class="initializer"> = time()</span></span> ] )</div> <p class="simpara"> The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in <em><code class="parameter">now</code></em>, or the current time if <em><code class="parameter">now</code></em> is not supplied. </p> <p class="para"> Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See <span class="function"><a href="function.date-default-timezone-get.html" class="function">date_default_timezone_get()</a></span> on the various ways to define the default time zone. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strtotime-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">time</code></em></span> <dd> <p class="para">A date/time string. Valid formats are explained in <a href="datetime.formats.html" class="link">Date and Time Formats</a>.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">now</code></em></span> <dd> <p class="para"> The timestamp which is used as a base for the calculation of relative dates. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strtotime-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a timestamp on success, <strong><code>FALSE</code></strong> otherwise. Previous to PHP 5.1.0, this function would return <em>-1</em> on failure. </p> </div> <div class="refsect1 errors" id="refsect1-function.strtotime-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Every call to a date/time function will generate a <strong><code>E_NOTICE</code></strong> if the time zone is not valid, and/or a <strong><code>E_STRICT</code></strong> or <strong><code>E_WARNING</code></strong> message if using the system settings or the <var class="varname"><var class="varname">TZ</var></var> environment variable. See also <span class="function"><a href="function.date-default-timezone-set.html" class="function">date_default_timezone_set()</a></span></p> </div> <div class="refsect1 changelog" id="refsect1-function.strtotime-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> Prior to PHP 5.3.0, relative time formats supplied to the <em><code class="parameter">time</code></em> argument of <span class="function"><strong>strtotime()</strong></span> such as <em>this week</em>, <em>previous week</em>, <em>last week</em>, and <em>next week</em> were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of <em>Monday</em> through <em>Sunday</em>. </td> </tr> <tr> <td>5.3.0</td> <td> Prior to PHP 5.3.0, <em>24:00</em> was not a valid format and <span class="function"><strong>strtotime()</strong></span> returned <strong><code>FALSE</code></strong>. </td> </tr> <tr> <td>5.2.7</td> <td> In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions. </td> </tr> <tr> <td>5.1.0</td> <td> Now returns <strong><code>FALSE</code></strong> on failure, instead of <em>-1</em>. </td> </tr> <tr><td>5.1.0</td><td><p class="para"> Now issues the <strong><code>E_STRICT</code></strong> and <strong><code>E_NOTICE</code></strong> time zone errors.</p></td></tr> <tr> <td>5.0.2</td> <td> In PHP 5 up to 5.0.2, <em>"now"</em> and other relative times are wrongly computed from today's midnight. This differs from other versions where it is correctly computed from current time. </td> </tr> <tr> <td>5.0.0</td> <td> Microseconds began to be allowed, but they are ignored. </td> </tr> <tr> <td>4.4.0</td> <td> In PHP versions prior to 4.4.0, <em>"next"</em> is incorrectly computed as +2. A typical solution to this is to use <em>"+1"</em>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.strtotime-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2323"> <p><strong>Example #1 A <span class="function"><strong>strtotime()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"10 September 2000"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 day"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 week"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 week 2 days 4 hours 2 seconds"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"next Thursday"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"last Monday"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-2324"> <p><strong>Example #2 Checking for failure</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Not Good'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// previous to PHP 5.1.0 you would compare with -1, instead of false<br /></span><span style="color: #007700">if ((</span><span style="color: #0000BB">$timestamp </span><span style="color: #007700">= </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"The string (</span><span style="color: #0000BB">$str</span><span style="color: #DD0000">) is bogus"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$str</span><span style="color: #DD0000"> == " </span><span style="color: #007700">. </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'l dS \o\f F Y h:i:s A'</span><span style="color: #007700">, </span><span style="color: #0000BB">$timestamp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.strtotime-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07). </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though. </p> <p class="para"> For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Dates in the <em>m/d/y</em> or <em>d-m-y</em> formats are disambiguated by looking at the separator between the various components: if the separator is a slash (<em>/</em>), then the American <em>m/d/y</em> is assumed; whereas if the separator is a dash (<em>-</em>) or a dot (<em>.</em>), then the European <em>d-m-y</em> format is assumed. </p> <p class="para"> To avoid potential ambiguity, it's best to use ISO 8601 (<em>YYYY-MM-DD</em>) dates or <span class="methodname"><a href="datetime.createfromformat.html" class="methodname">DateTime::createFromFormat()</a></span> when possible. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Using this function for mathematical operations is not advisable. It is better to use <span class="methodname"><a href="datetime.add.html" class="methodname">DateTime::add()</a></span> and <span class="methodname"><a href="datetime.sub.html" class="methodname">DateTime::sub()</a></span> in PHP 5.3 and later, or <span class="methodname"><a href="datetime.modify.html" class="methodname">DateTime::modify()</a></span> in PHP 5.2. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.strtotime-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="datetime.formats.html" class="link">Date and Time Formats</a></li> <li class="member"> <span class="methodname"><a href="datetime.createfromformat.html" class="methodname" rel="rdfs-seeAlso">DateTime::createFromFormat()</a> - Returns new DateTime object formatted according to the specified format</span></li> <li class="member"> <span class="function"><a href="function.checkdate.html" class="function" rel="rdfs-seeAlso">checkdate()</a> - Validate a Gregorian date</span></li> <li class="member"> <span class="function"><a href="function.strptime.html" class="function" rel="rdfs-seeAlso">strptime()</a> - Parse a time/date generated with strftime</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strptime.html">strptime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.time.html">time</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>