DateInterval::createFromDateString

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateInterval::createFromDateStringCria um DateInterval de porções relativas da string

Descrição

public static DateInterval::createFromDateString(string $time): DateInterval

Utiliza o interpretador de datas para criar um DateInterval a partir de porções relativas de uma string analisada.

Parâmetros

time

Uma data com porções relativas. Especificamente, os formatos relativos suportados pelo analisador utilizados em strtotime() e DateTime serão utilizados para construir o DateInterval.

Exemplos

Exemplo #1 Analisando intervalos de datas válidos

<?php
// Each set of intervals is equal.
$i = new DateInterval('P1D');
$i DateInterval::createFromDateString('1 day');

$i = new DateInterval('P2W');
$i DateInterval::createFromDateString('2 weeks');

$i = new DateInterval('P3M');
$i DateInterval::createFromDateString('3 months');

$i = new DateInterval('P4Y');
$i DateInterval::createFromDateString('4 years');

$i = new DateInterval('P1Y1D');
$i DateInterval::createFromDateString('1 year + 1 day');

$i = new DateInterval('P1DT12H');
$i DateInterval::createFromDateString('1 day + 12 hours');

$i = new DateInterval('PT3600S');
$i DateInterval::createFromDateString('3600 seconds');
?>

Valor Retornado

Retorna uma nova instância de DateInterval.

add a note

User Contributed Notes 3 notes

up
2
Anonymous
12 years ago
DateInterval::createFromDateString ( string $time )

When the manual says "Uses the normal date parsers" it means that this function cannot take $time = ISO8601 strings like "P7D".  If you want to use those, you must use the constructor.
up
0
BobRay
2 months ago
Weeks are always converted to days and added to any days specified. As far as I can tell, this is the only calculation done by the method. Somewhat surprisingly, this works (though obviously  it would be a poor coding practice):

$di = DateInterval::createFromDateString('62 weeks + 1 day + 2 weeks + 2 hours + 70 minutes');
  /* Stored as 0 years, 449 days, 2 hours, 70 minutes */
up
0
BobRay
2 months ago
Unlike the DateInterval constructor, this method will accept negative values, so this works:

      $di = DateInterval::createFromDateString('1 year - 10 days');
           /* Stored as 1 year, -10 days */
To Top