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: Fri, 24 Jun 2011

view this page in

DateInterval::__construct

(PHP 5 >= 5.3.0)

DateInterval::__constructCreates new DateInterval object

Description

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

Creates new DateInterval object.

Parameters

interval_spec

Interval specification.

The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.

interval_spec Period Designators
Period Designator Description
Y years
M months
D days
W weeks. These get converted into days, so can not be combined with D.
H hours
M minutes
S seconds

Here are some simple examples. Two days is P2D. Two seconds is PT2S. Six years and five minutes is P6YT5M.

Note:

The unit types must be entered from the largest scale unit on the left to the smallest scale unit on the right. So years before months, months before days, days before minutes, etc. Thus one year and four days must be represented as P1Y4D, not P4D1Y.

The specification can also be represented as a date time. A sample of one year and four days would be P0001-00-04T00:00:00. But the values in this format can not exceed a given period's roll-over-point (e.g. 25 hours is invalid).

These formats are based on the » ISO 8601 duration specification.

Examples

Example #1 DateInterval example

<?php

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

?>

The above example will output:

DateInterval Object
(
    [y] => 2
    [m] => 0
    [d] => 4
    [h] => 6
    [i] => 8
    [s] => 0
    [invert] => 0
    [days] => 0
)

See Also

  • DateInterval::format() - Formats the interval
  • DateTime::add() - اصافه کردن تعداد روز، ماه، سال، ساعت، دقیقه و ثانیه به شی DateTime
  • DateTime::sub() - کم کردن تعدادی روز، ماه، سال، ساعت، دقیقه و ثانیه به شی DateTime
  • DateTime::diff() - بازگرداندن تفاوت بین دو شی DateTime



DateInterval::createFromDateString> <DateInterval
[edit] Last updated: Fri, 24 Jun 2011
 
add a note add a note User Contributed Notes DateInterval::__construct - [5 notes]
up
5
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
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
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
4
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 | stats | sitemap | contact | advertising | mirror sites