hari vallabh shukla
hari vallabh shukla

Reputation: 109

why php datetime object compared with comparision operator

php compare two dateTime object with comparision perator which is >(greater than).Below code is given

    $date1=new DateTime("07-03-2010");
    $date2=new Datetime("21-09-2015");
    if($date1>$date2)
    {
       echo "two datetime object has compared";
     }

Above code give correct result.My question is how two Datetime object is compared with comparision operator in php

Upvotes: 2

Views: 390

Answers (3)

Nitesh Mishra
Nitesh Mishra

Reputation: 311

Actually in PHP, when we store dates in a variable, it doesn't stored in any format. rather it is stored in seconds i.e. the difference between your given date and a reference date that is defined by PHP. I don't exactly remember the reference date, you may google it.

So, when you compare dates, it actually compares seconds.

Look at this,

http://codepad.org/UvbPdpmG

Upvotes: 0

Ry-
Ry-

Reputation: 224913

DateTime is a builtin and can overload operators. (This isn’t possible in PHP… yet.)

I think the relevant line is ext/date/php_date.c:1995.

Upvotes: 3

moonwave99
moonwave99

Reputation: 22817

By design:

Built-in classes can define its own comparison

Upvotes: 1

Related Questions