If you want to convert a Timespan given in Seconds into an DateInterval Object you could dot the following:
<?php
$dv = new DateInterval('PT'.$timespan.'S');
?>
but wenn you look at the object, only the $dv->s property is set.
As stated in the documentation to DateInterval::format
The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
If you still want to calculate the seconds into hours / days / years, etc do the following:
<?php
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timespan.'S'));
$iv = $d2->diff($d1);
?>
$iv is an DateInterval set with days, years, hours, seconds, etc ...
La clase DateInterval
Introducción
Representación del intervalo de fecha. Un intervalo de fecha almacena una cantidad fija de tiempo (en años, meses, días, horas, etc.) o una cadena de tiempo relativa en el formato que el constructor DateTime soporta.
Clases sinopsis
DateInterval
{
/* Propiedades */
/* Métodos */
}Propiedades
- y
-
Número de años.
- m
-
Número de meses.
- d
-
Número de días.
- h
-
Número de horas.
- i
-
Número de minutos.
- s
-
Número de segundos.
- invert
-
Es 1 si el intervalo está invertido y 0 de otro modo. Véase DateInterval::format().
- days
-
Número total de días que abarca el intervalo. Si es desconocido, days será FALSE.
Table of Contents
- DateInterval::__construct — Crea un nuevo objeto DateInterval
- DateInterval::createFromDateString — Establece un objeto DateInterval desde las partes relativas de una cadena
- DateInterval::format — Formatea el intervalo
p dot scheit at ps-webforge dot com
15-Mar-2011 02:07
Anonymous
18-Feb-2011 08:42
You cannot use the ++ operator on the DateInterval fields; it has no effect on the field value. To increase a field value, you have to do it the long way, for example $diff->h = $diff->h + 1; instead of $diff->h++; (observed in PHP 5.3.1).
jeff dot davies at yahoo dot com
15-Sep-2010 12:15
This class became available in PHP 5.3. It is not present in 5.2 or earlier releases. I found this out the hard way when you PHP scripts stopped working when I deployed them onto a Yahoo server. Yahoo has 5.2 while my machine hosts 5.3.
sebastien dot michea at manaty dot net
30-Aug-2010 03:25
It would be nice that when converting a DateInterval to a string, the interval specification used to construct the object is returned (like "P2W").
I need this to serialize a DateInterval object in order to store it in a postgres DB.
