Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: strtotime is returning false for a future date?

here are some debug expressions i put into eclipse, if you don't believe me:

"strtotime("2110-07-16 10:07:47")" = (boolean) false    
"strtotime("2110-07-16")" = (boolean) false 

i'm using it in my function which returns a random date between the start and end dates:

public static function randomDate($start_date, $end_date, $format = DateTimeHelper::DATE_FORMAT_SQL_DATE)
    {
        if($start_date instanceof DateTime)     $start_date = $start_date->format(DateTimeHelper::DATE_FORMAT_YMDHMS);
        if($end_date instanceof DateTime)       $end_date   = $end_date->format(DateTimeHelper::DATE_FORMAT_YMDHMS);

        // Convert timetamps to millis
        $min = strtotime($start_date);
        $max = strtotime($end_date);

        // Generate random number using above bounds
        $val = rand($min, $max);

        // Convert back to desired date format
        return date($format, $val);
    }

any idea how to get it to return the right unix time for a future date?

thanks!

like image 486
Garrett Avatar asked Jul 16 '10 14:07

Garrett


1 Answers

If you want to work with dates that fall outside the 32-bit integer date range, then use PHP's dateTime objects

try {
    $date = new DateTime('2110-07-16 10:07:47');
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $date->format('Y-m-d');
like image 152
Mark Baker Avatar answered Sep 19 '22 00:09

Mark Baker