note 107254 added to class.datetime
| From: | gordon at kavi dot com | Date: | Tue, 24 Jan 2012 21:58:36 +0000 |
| Subject: | note 107254 added to class.datetime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185735@lists.php.net to get a copy of this message | ||
Here are some SimpleTest assertions for DateTime, all of which pass. This should shed some light on
how comparison operators work with DateTime objects in PHP.
/**
* Let's make some basic assertions about how DateTime comparison works,
* for demonstration purposes. (Turns out, the equality operators work
* like you'd think, at least on the surface. Note, however, that the
* operators compare the global time under the covers, and DO NOT take
* time zone into account; if two times are equal in UTC but have different
* time zones, they'll compare as equal.)
*/
public function test_compare_DateTime_objects() {
$utc1980_1 = new DateTime('1980-01-01 00:00:00', new
DateTimeZone('UTC'));
$utc1980_2 = new DateTime('1980-01-01 00:00:00', new
DateTimeZone('UTC'));
$est1980_1 = new DateTime('1980-01-01 00:00:00', new
DateTimeZone('US/Eastern'));
$est1980_minus5 = new DateTime('1979-12-31 19:00:00', new
DateTimeZone('US/Eastern')); // midnight UTC
$utc2000_1 = new DateTime('2000-01-01 00:00:00', new
DateTimeZone('UTC'));
$this->assertEqual($utc1980_1, $utc1980_2);
$this->assertTrue($utc1980_1 == $utc1980_2);
$this->assertFalse($utc1980_1 === $utc1980_2); // memory reference comparison should
fail
$this->assertNotEqual($utc1980_1, $est1980_1);
$this->assertFalse($utc1980_1 == $est1980_1); // note this is different in UTC
// the following assertion proves that for the == operator, only the UTC time is considered;
// 1979-12-31 19:00:00 EST == 1980-01-01 00:00:00 UTC is true, even though time zone differs
$this->assertTrue($utc1980_1 == $est1980_minus5);
$this->assertTrue($utc1980_2 == $est1980_minus5);
$this->assertFalse($utc1980_1 === $est1980_1);
$this->assertNotEqual($utc1980_1, $utc2000_1);
$this->assertFalse($utc1980_1 == $utc2000_1);
$this->assertFalse($utc1980_1 === $utc2000_1);
$this->assertFalse($utc1980_1 > $utc1980_2);
$this->assertFalse($utc1980_1 < $utc1980_2);
$this->assertTrue($utc1980_1 < $utc2000_1);
$this->assertFalse($utc1980_1 > $utc2000_1);
$this->assertFalse($utc2000_1 < $utc1980_1);
$this->assertTrue($utc2000_1 > $utc1980_1);
$this->assertFalse($utc1980_1 < $est1980_minus5);
$this->assertFalse($utc1980_1 > $est1980_minus5);
$this->assertTrue($utc2000_1 > $est1980_minus5);
$this->assertFalse($utc2000_1 < $est1980_minus5);
}
----
Server IP: 192.41.42.26
Probable Submitter: 64.242.113.237
----
Manual Page -- http://www.php.net/manual/en/class.datetime.php
Edit -- https://master.php.net/note/edit/107254
Del: integrated -- https://master.php.net/note/delete/107254/integrated
Del: useless -- https://master.php.net/note/delete/107254/useless
Del: bad code -- https://master.php.net/note/delete/107254/bad+code
Del: spam -- https://master.php.net/note/delete/107254/spam
Del: non-english -- https://master.php.net/note/delete/107254/non-english
Del: in docs -- https://master.php.net/note/delete/107254/in+docs
Del: other reasons-- https://master.php.net/note/delete/107254
Reject -- https://master.php.net/note/reject/107254
Search -- https://master.php.net/manage/user-notes.php