John Cargo
John Cargo

Reputation: 2121

PHP date returning blank for Year "2090" with strtotime

I have PHP version 5.3.10-1ubuntu3.10.

I am specifying date as "2090-11-29 05:00:00" and using strotime to print and compare it with Current date. But it returns "Blank Value"

Here is my Code :

<?php
    $dateTime = "2003-04-19 11:00:00";
    $mdate = strtotime($dateTime);
    $ndateTime = "2090-11-29 05:00:00";
    $ndate = strtotime($ndateTime);

    echo $ndate.' - <br />';

    if(($date > $mdate) && ($date < $ndate)) {
        echo $date.' is greater than  '.$ndate . ' First ';
    } else {
        echo $ndate.' is greater than  '.$date;
    }

    echo '<br />';
    echo phpversion();
?>

Upvotes: 0

Views: 312

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173662

You can use DateTime, which supports bigger dates than the regular time functions on 32-bit machines; its constructor supports the same format as strtotime() so conversion should be straightforward.

However, in this case a simple string comparison will accomplish the same thing, because the Y-m-d H:i:s format is suitable for comparisons of dates until Y10k.

$mdateTime = "2003-04-19 11:00:00";
$ndateTime = "2090-11-29 05:00:00";
$date = '2013-03-11';

if ($date > $mdateTime && $date < $ndateTime) {
    // $date is between $mdateTime and $ndateTime
} else {
    echo $ndate.' is greater than  '.$date;
}

Upvotes: 1

mamdouh alramadan
mamdouh alramadan

Reputation: 8528

the max date you can get is 2038, check the year 2038 problem for more information about this issue. Now to solve the problem -- as @Mike suggested -- you can use DateTime objects, consider the following example:

$date =new DateTime("2090-11-29 05:00:00");
echo $date->format('Y-m-d H:i:s'); //will output 2090-11-29 05:00:00

and you can set the format as you wish and there are enough examples found in the php documentation Also settimezone function for datetime object

Upvotes: 2

Related Questions