PHP:strtotime为未来的日期返回false? [英] PHP: strtotime is returning false for a future date?

查看:1086
本文介绍了PHP:strtotime为未来的日期返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些我放入eclipse的调试表达式,如果你不相信我:

 strtotime(2110 -07-16 10:07:47)=(boolean)false 
strtotime(2110-07-16)=(boolean)false
/ pre>

我在我的函数中使用它,它返回开始和结束日期之间的随机日期:

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

//将时间表转换为millis
$ min = strtotime($ start_date);
$ max = strtotime($ end_date);

//使用上边界生成随机数
$ val = rand($ min,$ max);

//转换回所需的日期格式
返回日期($ format,$ val);
}

任何想法如何让它为未来的日期返回正确的unix时间



谢谢!

解决方案

在32位整数日期范围之外,然后使用PHP的dateTime对象

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

echo $ date-> format('Y-m-d');


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!

解决方案

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');

这篇关于PHP:strtotime为未来的日期返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆