PHP将一个日期字符串转换成举例当前的天数
输入为一个日期字符串,例如:2011-3-23
输出为举例当前的天数,例如:1
?
立即学习“PHP免费学习笔记(深入)”;
代码为:
?
立即学习“PHP免费学习笔记(深入)”;
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怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
$day=floor((time()-strtotime($date))/86400);
$day=floor((time()-strtotime($date))/86400);
确实,strtotime是可以的,多谢指点。:-)
$day=floor((time()-strtotime($date))/86400);
确实,strtotime是可以的,多谢指点。:-)
而且,strtotime支持的日期格式很丰富,学习了。
$day=floor((time()-strtotime($date))/86400);
确实,strtotime是可以的,多谢指点。:-)
而且,strtotime支持的日期格式很丰富,学习了。
http://www.php.net/manual/en/datetime.formats.date.php