<!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>Relative Formats</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="datetime.formats.compound.html">Compound Formats</a></div> <div class="next" style="text-align: right; float: right;"><a href="timezones.html">List of Supported Timezones</a></div> <div class="up"><a href="datetime.formats.html">Supported Date and Time Formats</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="datetime.formats.relative" class="section"> <h2 class="title">Relative Formats</h2> <p class="para"> This page describes the different relative date/time formats that the <span class="function"><a href="function.strtotime.html" class="function">strtotime()</a></span>, <a href="class.datetime.html" class="classname">DateTime</a> and <span class="function"><a href="function.date-create.html" class="function">date_create()</a></span> parser understands. </p> <table class="doctable table"> <caption><strong>Used Symbols</strong></caption> <thead> <tr> <th>Description</th> <th>Format</th> </tr> </thead> <tbody class="tbody"> <tr> <td><em>dayname</em></td> <td>'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'</td> </tr> <tr> <td><em>daytext</em></td> <td>'weekday' | 'weekdays'</td> </tr> <tr> <td><em>number</em></td> <td>[+-]?[0-9]+</td> </tr> <tr> <td><em>ordinal</em></td> <td>'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' | 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'</td> </tr> <tr> <td><em>reltext</em></td> <td>'next' | 'last' | 'previous' | 'this'</td> </tr> <tr> <td><em>space</em></td> <td>[ \t]+</td> </tr> <tr> <td><em>unit</em></td> <td>(('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' | <em>daytext</em></td> </tr> </tbody> </table> <table class="doctable table"> <caption><strong>Day-based Notations</strong></caption> <thead> <tr> <th>Format</th> <th>Description</th> <th>Examples</th> </tr> </thead> <tbody class="tbody"> <tr> <td>'yesterday'</td> <td>Midnight of yesterday</td> <td>"yesterday 14:00"</td> </tr> <tr> <td>'midnight'</td> <td>The time is set to 00:00:00</td> <td class="empty"> </td> </tr> <tr> <td>'today'</td> <td>The time is set to 00:00:00</td> <td class="empty"> </td> </tr> <tr> <td>'now'</td> <td>Now - this is simply ignored</td> <td class="empty"> </td> </tr> <tr> <td>'noon'</td> <td>The time is set to 12:00:00</td> <td>"yesterday noon"</td> </tr> <tr> <td>'tomorrow'</td> <td>Midnight of tomorrow</td> <td class="empty"> </td> </tr> <tr> <td>'back of' <em>hour</em></td> <td>15 minutes past the specified hour</td> <td>"back of 7pm", "back of 15"</td> </tr> <tr> <td>'front of' <em>hour</em></td> <td>15 minutes before the specified hour</td> <td>"front of 5am", "front of 23"</td> </tr> <tr> <td>'first day' ' of'?</td> <td>Sets the day of the first of the current month. This phrase is best used together with a month name following it.</td> <td>"first day of January 2008"</td> </tr> <tr> <td>'last day' ' of'?</td> <td>Sets the day to the last day of the current month. This phrase is best used together with a month name following it.</td> <td>"last day of next month"</td> </tr> <tr> <td><em>ordinal</em> <em>space</em> <em>dayname</em> <em>space</em> 'of'</td> <td>Calculates the <em>x</em>-th week day of the current month.</td> <td>"first sat of July 2008"</td> </tr> <tr> <td>'last' <em>space</em> <em>dayname</em> <em>space</em> 'of'</td> <td>Calculates the <em class="emphasis">last</em> week day of the current month.</td> <td>"last sat of July 2008"</td> </tr> <tr> <td><em>number</em> <em>space</em>? (<em>unit</em> | 'week')</td> <td>Handles relative time items where the value is a number.</td> <td>"+5 weeks", "12 day", "-7 weekdays"</td> </tr> <tr> <td><em>ordinal</em> <em>space</em> <em>unit</em></td> <td>Handles relative time items where the value is text.</td> <td>"fifth day", "second month"</td> </tr> <tr> <td>'ago'</td> <td>Negates all the values of previously found relative time items.</td> <td>"2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago ago"</td> </tr> <tr> <td><em>dayname</em></td> <td>Moves to the next day of this name.</td> <td>"Monday"</td> </tr> <tr> <td><em>reltext</em> <em>space</em> 'week'</td> <td>Handles the special format "weekday + last/this/next week".</td> <td>"Monday next week"</td> </tr> </tbody> </table> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Relative statements are always processed <em class="emphasis">after</em> non-relative statements. This makes "+1 week july 2008" and "july 2008 +1 week" equivalent. </p> <p class="para"> Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today's date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Observe the following remarks when the current day-of-week is the same as the day-of-week used in the date/time string. The current day-of-week could have been (re-)calculated by non-relative parts of the date/time string however. </p> <ol type="1"> <li class="listitem"> <span class="simpara"> "<em>dayname</em>" does <em class="emphasis">not</em> advance to another day. (Example: "Wed July 23rd, 2008" means "2008-07-23"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>number</em> <em>dayname</em>" does <em class="emphasis">not</em> advance to another day. (Example: "1 wednesday july 23rd, 2008" means "2008-07-23"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>number</em> week <em>dayname</em>" will first add the number of weeks, but does <em class="emphasis">not</em> advance to another day. In this case "<em>number</em> week" and "<em>dayname</em>" are two distinct blocks. (Example: "+1 week wednesday july 23rd, 2008" means "2008-07-30"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>ordinal</em> <em>dayname</em>" <em class="emphasis">does</em> advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>number</em> week <em>ordinal</em> <em>dayname</em>" will first add the number of weeks, and then <em class="emphasis">advances</em> to another day. In this case "<em>number</em> week" and "<em>ordinal</em> <em>dayname</em>" are two distinct blocks. (Example: "+1 week first wednesday july 23rd, 2008" means "2008-08-06"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>ordinal</em> <em>dayname</em> 'of' " does <em class="emphasis">not</em> advance to another day. (Example: "first wednesday of july 23rd, 2008" means "2008-07-02" because the specific phrase with 'of' resets the day-of-month to '1' and the '23rd' is ignored here). </span> </li> </ol> <p class="para"> Also observe that the "of" in "<em>ordinal</em> <em>space</em> <em>dayname</em> <em>space</em> 'of' " and "'last' <em>space</em> <em>dayname</em> <em>space</em> 'of' " does something special. </p> <ol type="1"> <li class="listitem"> <span class="simpara"> It sets the day-of-month to 1. </span> </li> <li class="listitem"> <span class="simpara"> "<em>ordinal</em> <em>dayname</em> 'of' " does <em class="emphasis">not</em> advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01"). </span> </li> <li class="listitem"> <span class="simpara"> "<em>ordinal</em> <em>dayname</em> " <em class="emphasis">does</em> advance to another day. (Example: "first tuesday july 2008" means "2008-07-08", see also point 4 in the list above). </span> </li> <li class="listitem"> <span class="simpara"> "'last' <em>dayname</em> 'of' " takes the last <em>dayname</em> of the current month. (Example: "last wed of july 2008" means "2008-07-30") </span> </li> <li class="listitem"> <span class="simpara"> "'last' <em>dayname</em>" takes the last <em>dayname</em> from the current day. (Example: "last wed july 2008" means "2008-06-25"; "july 2008" first sets the current date to "2008-07-01" and then "last wed" moves to the previous Wednesday which is "2008-06-25"). </span> </li> </ol> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days. </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="datetime.formats.compound.html">Compound Formats</a></div> <div class="next" style="text-align: right; float: right;"><a href="timezones.html">List of Supported Timezones</a></div> <div class="up"><a href="datetime.formats.html">Supported Date and Time Formats</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>