Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

datetime - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to keep it before Tue, 19 Jan 2038 03:14:07 UTC, when the unix timestamp epoch for 32 bit systems rolls over!

It's even described in the manual at http://php.net/strtotime

edit: Just tested: It's fixed by installing a 64 bit OS and appropriate 64 bit version of php. I guess we have time enough to fix a reincarnated millenium bug:

$one = strtotime("9999-12-31 23:59:59");  
$two = strtotime("10000-01-01 00:00:00");
var_dump($one);
var_dump($two);

int(253402297199)
bool(false)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...