输入为一个日期字符串,例如:2011-3-23
输出为举例当前的天数,例如:1
代码为:
public static function convertDateToLong($dateStr){
$checkPattern = "/^\d{4}(((-\d{1,2}){2})|((\.\d{1,2}){2})|((\/\d{1,2}){2}))$/";
$date = substr(trim($dateStr),0,strpos(trim($dateStr)," ")>0 ? strpos(trim($dateStr)," ") : strlen(trim($dateStr)));
if(preg_match($checkPattern,$date)){
preg_match("/([-\/.])/",$date,$outer);
$dilimeter = $outer[1];
list($year,$month,$day) = explode($dilimeter,$date);
if(checkdate($month,$day,$year)){
$spsec = time()-mktime(0,0,0,$month,$day,$year);
if($spsec < 0) throw new Exception("date can not be after today!!!");
$spday = floor($spsec/24/60/60);
return $spday;
}
else{
throw new Exception("the date input is not a valid date");
}
}
else{
throw new Exception("the dateStr is wrong formatted!!!");
}
}

本文介绍了一个用于将日期字符串转换为距离当前天数的PHP函数。该函数可以解析多种格式的日期字符串,并计算出输入日期与当前日期之间的天数差。
2884

被折叠的 条评论
为什么被折叠?



