如果参数也为0,则strtotime()应返回0

时间:2015-05-07 08:13:37

标签: php date strtotime

如果参数也为0,如何让函数strtotime()返回0?

在我的例子中,$ testvar2返回“empty”。

$testvar = 0;
$testvar2 = strtotime($testvar);

print_r($testvar2);

5 个答案:

答案 0 :(得分:4)

它没有返回空,它返回你bool(false)。请查看var_dump而不是print_r。

解决方法:

$testvar2 = ($testvar) ? strtotime($testvar) : 0;

答案 1 :(得分:3)

只需简单地设置ternary operator,因为strtotime()如果失败则返回false,您只需返回0,否则返回正确的值。

$testvar2 = strtotime($testvar)?:0;

答案 2 :(得分:0)

只需添加对该变量的检查(0,false,null,not set等)。试试 -

$testvar2= (!empty($testvar)) ? strtotime($testvar) : 0;

答案 3 :(得分:0)

function getTime($testvar){
    return ($testvar == 0)? 0 : strtotime($testvar);
}

答案 4 :(得分:-1)

public static function myStrToTime($var=null)
{
    if($var)
        return strtotime($var);
    return 0;
}

没有冒犯,但也许你应该查阅一些关于PHP的教程。 :)

相关问题