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"));
DateInterval::__construct
(PHP 5 >= 5.3.0)
DateInterval::__construct — Yeni bir DateInterval nesnesi oluşturur
Açıklama
$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çleriBelirteç 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
Alternatively you can use DateInterval::createFromDateString() for negative intervals:
<?php
$date = new DateTime();
$date->add(DateInterval::createFromDateString('-89 days'));
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.
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.
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
