downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

DateInterval::createFromDateString> <DateInterval
[edit] Last updated: Wed, 19 Jun 2013

view this page in

DateInterval::__construct

(PHP 5 >= 5.3.0)

DateInterval::__constructCrée un nouvel objet DateInterval

Description

public DateInterval::__construct() ( string $interval_spec )

Crée un nouvel objet DateInterval.

Liste de paramètres

interval_spec

Une spécification d'intervalle.

Le format commence avec la lettre P, pour "period". Chaque durée de la période est représentée par une valeur entière suivie par une désignation de période. Si la durée contient des éléments de temps, cette portion de la spécification est précédée par la lettre T.

Désignation de période interval_spec
Désignation de période Description
Y Années
M Mois
D Jours
W Semaine. Sera converti en jours, aussi, vous ne pouvez pas le combiner avec D.
H Heures
M Minutes
S Secondes

Voici quelques exemples simples : 2 jours sera P2D ; 2 secondes sera PT2S ; 6 années et 5 minutes sera P6YT5M.

Note:

Les types d'unité doivent être entrés des plus grands aux plus petits. Ainsi, les années doivent être présentes avant les mois, les mois avant les jours, les jours avant les minutes, etc. Aussi, une année et 4 jours doit être représenté comme P1Y4D, et non P4D1Y.

Cette spécification peut également être représentée sous la forme d'une durée. Aussi, une année et 4 jours peut être P0001-00-04T00:00:00. Mais les valeurs de ce format ne peuvent pas exécéder une période donnée (i.e. 25 heures est invalide).

Ces formats sont basés sur la » spécification de durée ISO 8601.

Erreurs / Exceptions

Lance une exception Exception lorsque le paramètre interval_spec ne peut être analysé comme un intervalle.

Exemples

Exemple #1 Exemple avec DateInterval

<?php

$interval 
= new DateInterval('P2Y4DT6H8M');
var_dump($interval);

?>

L'exemple ci-dessus va afficher :

object(DateInterval)#1 (8) {
  ["y"]=>
  int(2)
  ["m"]=>
  int(0)
  ["d"]=>
  int(4)
  ["h"]=>
  int(6)
  ["i"]=>
  int(8)
  ["s"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  bool(false)
}

Voir aussi



DateInterval::createFromDateString> <DateInterval
[edit] Last updated: Wed, 19 Jun 2013
 
add a note add a note User Contributed Notes DateInterval::__construct - [5 notes]
up
2
daniellehr at gmx dot de
1 year ago
Alternatively you can use DateInterval::createFromDateString() for negative intervals:

<?php
$date
= new DateTime();
$date->add(DateInterval::createFromDateString('-89 days'));
up
1
buvinghausen at gmail dot com
1 year ago
I think it is easiest if you would just use the sub method on the DateTime class.

<?php
$date
= new DateTime();
$date->sub(new DateInterval("P89D"));
up
1
jawzx01 at gmail dot com
1 year ago
As previously mentioned, to do a negative DateInterval object, you'd code:

<?php
$date1
= new DateTime();
$eightynine_days_ago = new DateInterval( "P89D" );
$eightynine_days_ago->invert = 1; //Make it negative.
$date1->add( $eightynine_days_ago );
?>

and then $date1 is now 89 days in the past.
up
-1
kuzb
2 years ago
It should be noted that this class will not calculate days/hours/minutes/seconds etc given a value in a single denomination of time.  For example:

<?php
    $di
= new DateInterval('PT3600S');
    echo
$di->format('%H:%i:%s');
   
?>

will yield 0:0:3600 instead of the expected 1:0:0
up
0
kevinpeno at gmail dot com
2 years ago
Note that, while a DateInterval object has an $invert property, you cannot supply a negative directly to the constructor similar to specifying a negative in XSD ("-P1Y"). You will get an exception through if you do this.

Instead you need to construct using a positive interval ("P1Y") and the specify the $invert property === 1.

 
show source | credits | sitemap | contact | advertising | mirror sites