Formats relatifs

Cette page décrit les différents formats relatifs que les analyseurs des fonctions strtotime(), DateTime et date_create() comprennent.

Symboles utilisés
Description Format
dayname 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'
daytext 'weekday' | 'weekdays'
number [+-]?[0-9]+
ordinal 'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' | 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'
reltext 'next' | 'last' | 'previous' | 'this'
space [ \t]+
unit (('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' | daytext
Notations basées sur le jour
Format Description Exemples
'yesterday' Minuit d'hier "yesterday 14:00"
'midnight' Le temps est affecté à 00:00:00  
'today' Le temps est affecté à 00:00:00  
'now' Maintenant  
'noon' Le temps est affecté à 12:00:00 "yesterday noon"
'tomorrow' Minuit de demain  
'back of' hour 15 minutes avant l'heure précisée "back of 7pm", "back of 15"
'front of' hour 15 minutes après l'heure spécifiée "front of 5am", "front of 23"
'first day of'? Affecte le jour du premier jour du mois courant. Il est plus intéressant d'utiliser cette phrase suivie d'un nom de mois. "first day of January 2008"
'last day of'? Affecte le jour du dernier jour du mois courant. Il est plus intéressant d'utiliser cette phrase suivie d'un nom de mois. "last day of next month"
ordinal space dayname space 'of' Calcule le x-ème jour de semaine du mois en cours. "first sat of July 2008"
'last' space dayname space 'of' Calcule le dernier jour de semaine du mois en cours. "last sat of July 2008"
number space? (unit | 'week') Gère des temps relatifs dont la valeur est dénombrée. "+5 weeks", "12 day", "-7 weekdays"
ordinal space unit Gère des temps relatifs dont la valeur est dénombrée. "fifth day", "second month"
'ago' Utilise dans le passé toute description de temps relatif ('il y a'). "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago"
dayname Se déplace vers le prochain jour indiqué. "Monday"
reltext space 'week' Gère le format spécial "weekday + last/this/next week". "Monday next week"

Note:

Les expressions relatives sont toujours traitées après les expressions non relatives. Ceci fait en sorte que "+1 week july 2008" et "july 2008 +1 week" sont équivalents.

"yesterday", "midnight", "today", "noon" et "tomorrow" sont des exceptions à cette règle. Notez que "tomorrow 11:00" et "11:00 tomorrow" sont différents. Soit la date d'aujourd'hui à "July 23rd, 2008", la première expression donne "2008-07-24 11:00" alors que la seconde donnera "2008-07-24 00:00". La raison est que ces cinq expressions influencent directement la date courante.

Note:

Notez les remarques qui suivent lorsque le jour de semaine courant est le même que le jour de semaine utilisé dans la chaine de date/time. Le jour de semaine courant aurait pu être recalculé par rapport aux parties non relatives de la chaine date/time.

  1. "dayname" n'avance pas vers un autre jour. (Exemple : "Wed July 23rd, 2008" signifie "2008-07-23").
  2. "number dayname" n'avance pas vers un autre jour. (Exemple : "1 wednesday july 23rd, 2008" signifie "2008-07-23").
  3. "number week dayname" ajoutera d'abord le nombre de semaines, mais n'avancera pas vers un autre jour. Dans ce cas "number week" et "dayname" sont deux blocs distincts. (Exemple : "+1 week wednesday july 23rd, 2008" signifie "2008-07-30").
  4. "ordinal dayname" avance vers un autre jour. (Exemple : "first wednesday july 23rd, 2008" signifie "2008-07-30").
  5. "number week ordinal dayname" ajoutera d'abord le nombre de semaines et ensuite avancera vers un autre jour. Dans ce cas, "number week" et "ordinal dayname" sont deux blocs distincts. (Exemple : "+1 week first wednesday july 23rd, 2008" signifie "2008-08-06").
  6. "ordinal dayname 'of' " n'avance pas vers un autre jour. (Exemple : "first wednesday of july 23rd, 2008" signifie "2008-07-02" car la phrase avec 'of' remet à zéro le jour du mois vers '1' et le '23' sera ignoré).

Notez aussi que le "of" dans "ordinal space dayname space 'of' " et "'last' space dayname espace 'of' " fait quelque chose de spécial.

  1. Il affecte le jour du mois à 1.
  2. "ordinal dayname 'of' " n'avance pas vers un autre jour. (Exemple : "first tuesday of july 2008" signifie "2008-07-01").
  3. "ordinal dayname " avance vers un autre jour. (Exemple : "first tuesday july 2008" signifie "2008-07-08", voyez aussi le point numéro 4 de la liste ci-dessus).
  4. "'last' dayname 'of' " prend le dernier dayname du mois courant. (Exemple : "last wed of july 2008" signifie "2008-07-30")
  5. "'last' dayname" prend le dernier dayname à partir du jour actuel. (Exemple : "last wed july 2008" signifie "2008-06-25"; "july 2008" affecte d'abord la date courante à "2008-07-01" et ensuite "last wed" remonte au dernier mercredi qui est le "2008-06-25").

Note:

Les valeurs relatives des mois calculés sont basées sur le nombre de jours des mois que l'on utilise. Par exemple, "+2 month 2011-11-30", donnera comme résultat la date "2012-01-30". Ceci est du au fait que le mois de novembre comporte 30 jours, et que le mois de décembre en comporte 31, soit un ajout total de 61 jours.

Note:

number est un nombre entier ; si un nombre décimal est donné, le point (ou la virgule) sera probablement interprété comme un délimiteur. Par exemple, '+1.5 hours' sera analysé en tant que '+1 5 hours', et non pas comme '+1 hour +30 minutes'.

Historique

Version Description
7.0.8 Les semaines commencent toujours le lundi. Autrefois, le dimanche était également considéré pour commencer une semaine.

add a note add a note

User Contributed Notes 13 notes

up
46
Mark Simon
9 years ago
Note that expressions such as “last day of” and “first day of” imply a day of a month, not, for example of the year or week.

So, expressions, such as “first day of this year” will give the first day of this month, with no apparent regard for the year.

As powerful as the parser is, it can lead to disappointing or confusing results.
up
22
Igor
6 years ago
@Mark Simon, while you're correct, it's still possible to get first/last day of the year using relative date formats, though in a somewhat roundabout way.
<?php
   
echo gmdate('F j, Y', strtotime('first day of january this year'));;
   
/* Output: January 1, 2018 */

   
echo gmdate('F j, Y', strtotime('last day of december this year'));;
   
/* Output: December 31, 2018 */
?>
up
4
InvisibleSmiley
4 years ago
Both dayname AND daytext ("weekday"/"weekdays") reset time information to 0.

Depending on how accurate you need it, the following may work for you:

new DateTime('last weekday ' . date('H:i:s'));
new DateTime('last weekday ' . (new DateTime())->format('H:i:s.u'));

(As documented, date() doesn't handle microseconds.)
up
19
oguzkonya34 at gmail dot com
12 years ago
April 1st, 2012 is Sunday. You might expect to get April 2nd, 2012 with 'Monday next week', however this:

<?php
   
echo date('F jS, Y', strtotime('Monday next week 2012-04-01'));
?>

returns April 9th, 2012. To get April 2nd, you need to use this:

<?php
   
echo date('F jS, Y', strtotime('next Monday 2012-04-01'));
?>

Apparently 'next week' advances the week if and only if the day is Sunday. This:

<?php
   
echo date('F jS, Y', strtotime('Monday next week 2012-03-31'));
?>

would still return April 2nd.
up
5
phpnet at rudde dot org
8 years ago
Get the first day of a month, in a typical calendar month, typically the Monday in the first week of the month. In my example when the code is ran at 2016-04-06, 1st of april will be a Friday, since Calendars typically start on monday, this is the date you might want, in this case this would be 28th of March.

<?php
/* Code ran at 2016-04-06 */
$date = new DateTime('first day of this month');
$date->modify('monday this week');

echo
$date->format('Y-m-d');
/* Output: 2016-03-28 */
?>
up
10
geneticdrift
13 years ago
Month skipping has issues with number of days in the month.

'+1 month' may move to the following month if the next month has less days than the current:

<?php print date('d M Y H:i:s', strtotime('+1 month', strtotime('Thu Mar 31 19:50:41 IST 2011'))); ?>
Prints: 01 May 2011 19:50:41

But with 'last day of +1 month' the result is different:

<?php print date('d M Y H:i:s', strtotime('last day of +1 month', strtotime('Thu Mar 31 19:50:41 IST 2011'))); ?>
Prints: 30 Apr 2011 19:50:41

(PHP 5.3.3)
up
5
chris at burnhamup dot com
11 years ago
Using strtotime('DAYNAME') like strtotime('Tuesday') returns midnight on the specified day. It doesn't preserve the current time or the time passed into the function.
up
0
yaroslav429 at gmail dot com
4 years ago
@Igor, @Mark Simon, seems that was fixed.

PHP 7.4.10
<?php
new DateTimeImmutable('midnight first day of');
/* date: 2020-09-01 00:00:00.0 UTC (+00:00) */

new DateTimeImmutable('midnight first day of next month next year');
/* date: 2021-10-01 00:00:00.0 UTC (+00:00) */
?>
up
0
php at rcs dot us
7 years ago
I'm not sure if this is a bug or expected. As erwin points out :

"first monday of may 2011" is invalid, but "first monday may 2011" does work. Simply omit "of" if above examples do not work.

This still applies in 5.5 as well, and I ran into it while building out a utility to check if a day was a holiday.

Meaning "fourth day July 2016" /works/, but "fourth day of July 2016" does not. It will return an error. I emphasize works, because "fourth day July 2016" actually returns 2016-07-05. So you'll need "third day July 2016" for the Fourth of July.

However, "Last Monday May 2016" and "Last Monday of May 2016" ( for Memorial Day ) are both /valid/, but produce different results. The first returns 2016-04-25, which is incorrect, while the second returns the correct 2016-05-30.
up
-1
matt
13 years ago
If the ordinal being used is "last" and the next month is n, use the "n 1". Otherwise, if the previous month is p and it has d number of days in it, use "p d".

For example: "last thursday of November" becomes "last thursday December 1" and "fourth thursday of November" becomes "fourth thursday September 30th"

Hopes this helps someone out there.
up
-16
Pseudoname
9 years ago
oguzkonya34 at gmail dot com
Point of correction:
Apparently 'next week' advances the week if and only if the day is Sunday. This:
<?php
   
echo date('F jS, Y', strtotime('Monday next week 2012-03-31'));
?>
would still return April 2nd.
Quote "
Apparently 'next week' advances the week if and only if the day is Sunday. "
Not really. 2012-03-31 would be Saturday if 2012-04-01 is Sunday as you mentioned. Then Monday next week for 2012-03-31 is April 2nd which is correct.
up
-4
mike
4 years ago
in case of somone wonder 'last day of 1 month ago' is also posible (not only month names)
up
-20
scragar at gmail dot com
13 years ago
Just for those who want the last day of the year without calling the function twice:

<?php
echo date('Y-m-d',
   
strToTime('1/1 next year -1 day')
);
//   2011-12-31
?>

For reference this is actually saying "Take the first day of the first month next year, then subtract 1 day".
To Top