User Contributed Notes 11 notes

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:

invert flag is unreliable. If you've created interval with \DateInterval::createFromDateString with value like '1 day ago' than actually days counter will be negative, and invert flag will be 0. Also, setting invert to 1 with negative units is not working.Reliable solution to check if interval is negative is to actually apply it and compare:<?phpprivate function isNegative(\DateInterval $interval) {$now = new \DateTimeImmutable();$newTime = $now->add($interval); return $newTime < $now; } ?>Also, if you want to compare some units of two intervals you should take abs() of them. Or make whole interval absolute:<?phpprivate function absInterval(\DateInterval $interval) {$now = new \DateTimeImmutable();$new = $now->add($interval);$newInt = $now->diff($new); if (1 === $newInt->invert) {$newInt->invert = 0; } return $newInt; }?>P.S.: tested on 5.5.12-dev and 5.5.9

class MyDateInterval extends DateInterval { public$pluralCheck = '()',// Must be exactly 2 characters long // The first character is the opening brace, the second the closing brace // Text between these braces will be used if > 1, or replaced with $this->singularReplacement if = 1$singularReplacement = '',// Replaces $this->pluralCheck if = 1 // hour(s) -> hour$separator = ', ',// Delimiter between units // 3 hours, 2 minutes$finalSeparator = ', and ',// Delimeter between next-to-last unit and last unit // 3 hours, 2 minutes, and 1 second$finalSeparator2 = ' and ';// Delimeter between units if there are only 2 units // 3 hours and 2 minutes

public function formatWithoutZeroes () {// Each argument may have only one % parameter // Result does not handle %R or %r -- but you can retrieve that information using $this->format('%R') and using your own logic$parts = array (); foreach (func_get_args() as $arg) {$pre = mb_substr($arg, 0, mb_strpos($arg, '%'));$param = mb_substr($arg, mb_strpos($arg, '%'), 2);$post = mb_substr($arg, mb_strpos($arg, $param)+mb_strlen($param));$num = intval(parent::format($param));

You can create a series of dates starting with the first day of the week for each week, if you wish to populate list box on your web page with this date math. Use the absolute abs( ) function to convert negative numbers generated from dates in the past.<?php $TwoWeeksAgo = new DateTime(date("Ymd"));$TwoWeeksAgo->sub(new DateInterval('P'.abs ( (7-date("N")-14)).'D'));$LastWeek = new DateTime(date("Ymd"));$LastWeek->sub(new DateInterval('P'.abs ( (7-date("N")-7)).'D'));$ThisWeek = new DateTime(date("Ymd"));$ThisWeek->add(new DateInterval('P'.abs ( (7-date("N"))).'D'));

echo 'Start of This week is '.$ThisWeek->format('l m/d/Y').'<br/>'; echo 'Start of Last week is '.$LastWeek->format('l m/d/Y').'<br/>'; echo 'Start of 2 weeks ago is '.$TwosWeekAgo->format('l m/d/Y').'<br/>';?>