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

search for in the

DateInterval::createFromDateString> <DateInterval
[edit] Last updated: Fri, 23 Mar 2012

view this page in

DateInterval::__construct

(PHP 5 >= 5.3.0)

DateInterval::__constructYeni bir DateInterval nesnesi oluşturur

Açıklama

public DateInterval::__construct() ( string $zaman_aralığı )

Yeni bir DateInterval nesnesi oluşturur.

Değiştirgeler

zaman_aralığı

Dizge türünde zaman aralığı belirtimi.

Zaman aralığını ("period") belirtmek için biçem P harfiyle başlar, ardından süreyi belirten bir tamsayı değer gelir. Süre zaman bileşenleri içeriyorsa bunların önüne T harfi getirilir..

zaman_aralığı Belirteçleri
Belirteç Açıklama
Y Yıl
M Ay
D Gün
W Hafta. Gün sayısına dönüştürmek için D harfiyle birlikte belirtilebilir.
H Saat
M Dakika
S saniye

Burada bazı basit örneklere yer verilmiştir. İki gün: P2D. İki saniye: PT2S. Altı yıl, 5 dakika: P6YT5M.

Bilginize:

Birimler belirtilirken en büyük ölçekli birim solda en küçük ölçekli birim sağda kalacak şekilde veri girilir. Dolayısıyla, örneğin, aylar yıllardan sonra, günlerden önce yer alır. Bur yıl dört gün P4D1Y değil P1Y4D olarak ifade edilir.

Belirtim bir tarih saat olarak da gösterilebilir. Örneğin, bir yıl dört gün P0001-00-04T00:00:00 olarak gösterilebilir. Fakat bu biçemde belirtilen değerler birime tanınan azami değeri aşmamalıdır (örneğin 25 saat geçersizdir).

Bu biçemler » ISO 8601 süre belirtimine dayanır.

Örnekler

<?php

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

?>

Yukarıdaki örneğin çıktısı:

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

Ayrıca Bakınız

  • DateInterval::format() - Zaman aralığını biçimler
  • DateTime::add() - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
  • DateTime::diff() - Returns the difference between two DateTime objects



DateInterval::createFromDateString> <DateInterval
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes DateInterval::__construct
buvinghausen at gmail dot com 09-Apr-2012 06:50
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"));
daniellehr at gmx dot de 21-Mar-2012 01:06
Alternatively you can use DateInterval::createFromDateString() for negative intervals:

<?php
$date
= new DateTime();
$date->add(DateInterval::createFromDateString('-89 days'));
jawzx01 at gmail dot com 21-Feb-2012 10:43
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.
kevinpeno at gmail dot com 17-Mar-2011 11:47
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.
kuzb 04-Feb-2011 04:42
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

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