xref: /php-src/ext/date/tests/bug50055-002.phpt (revision b7860cd5)
1--TEST--
2Bug #50555 (DateTime::sub() allows 'relative' time modifications) (OO).
3--FILE--
4<?php
5$now = '2010-03-07 13:21:38 UTC';
6//positive DateInterval
7$da1 = date_create( $now );
8$ds1 = date_create( $now );
9$i = DateInterval::createFromDateString('third Tuesday of next month');
10echo $da1->format( DateTime::ISO8601 ), "\n";
11echo date_add($da1, $i)->format( DateTime::ISO8601 ), "\n";
12try {
13	$ds1->sub($i);
14} catch (DateInvalidOperationException $e) {
15	echo $e::class, ': ', $e->getMessage(), "\n";
16}
17
18//negative DateInterval
19$da2 = date_create( $now );
20$ds2 = date_create( $now );
21$i2 = DateInterval::createFromDateString('third Tuesday of last month');
22echo $da2->format( DateTime::ISO8601 ), "\n";
23echo date_add($da2, $i2)->format( DateTime::ISO8601 ), "\n";//works
24try {
25	$ds2->sub($i);
26} catch (DateInvalidOperationException $e) {
27	echo $e::class, ': ', $e->getMessage(), "\n";
28}
29?>
30--EXPECTF--
312010-03-07T13:21:38+0000
322010-04-20T13:21:38+0000
33DateInvalidOperationException: DateTime::sub(): Only non-special relative time specifications are supported for subtraction
342010-03-07T13:21:38+0000
352010-02-16T13:21:38+0000
36DateInvalidOperationException: DateTime::sub(): Only non-special relative time specifications are supported for subtraction
37