How can I get the function strtotime()
to return 0 if the argument is also 0?
In my example, $testvar2 returns "empty".
$testvar = 0;
$testvar2 = strtotime($testvar);
print_r($testvar2);
How can I get the function strtotime()
to return 0 if the argument is also 0?
In my example, $testvar2 returns "empty".
$testvar = 0;
$testvar2 = strtotime($testvar);
print_r($testvar2);
Just simply make a ternary operator, since strtotime()
will return false if it fails you can simply return 0 otherwise it will return the correct value.
$testvar2 = strtotime($testvar)?:0;
Simply add a check for that variable(0, false, null, not set etc.). Try with -
$testvar2= (!empty($testvar)) ? strtotime($testvar) : 0;
function getTime($testvar){
return ($testvar == 0)? 0 : strtotime($testvar);
}
public static function myStrToTime($var=null)
{
if($var)
return strtotime($var);
return 0;
}
No offense, but maybe you should look up some more tutorials on PHP. :)