PHP将一个日期字符串转换成举例来说当前的天数
PHP将一个日期字符串转换成举例当前的天数
输入为一个日期字符串,例如: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 <p></p><div class="comment_title"> 1 楼 bardo 2011-03-26 <div class="comment_content">丢开有效性检验不说,这个代码绕的弯子太大了。算天数只要一代码:<br>$day=floor((time()-strtotime($date))/86400);<p></p> <div class="comment_title"> 2 楼 liuzhiqiangruc 2011-03-30 <div class="comment_content"><div class="quote_title">bardo 写道<div class="quote_div">丢开有效性检验不说,这个代码绕的弯子太大了。算天数只要一代码:<br>$day=floor((time()-strtotime($date))/86400);<br>确实,strtotime是可以的,多谢指点。:-)<p></p> <div class="comment_title"> 3 楼 liuzhiqiangruc 2011-03-30 <div class="comment_content"><div class="quote_title">liuzhiqiangruc 写道<div class="quote_div"><div class="quote_title">bardo 写道<div class="quote_div">丢开有效性检验不说,这个代码绕的弯子太大了。算天数只要一代码:<br>$day=floor((time()-strtotime($date))/86400);<br>确实,strtotime是可以的,多谢指点。:-)<br>而且,strtotime支持的日期格式很丰富,学习了。<p></p> <div class="comment_title"> 4 楼 liuzhiqiangruc 2011-03-30 <div class="comment_content"><div class="quote_title">liuzhiqiangruc 写道<div class="quote_div"><div class="quote_title">liuzhiqiangruc 写道<div class="quote_div"><div class="quote_title">bardo 写道<div class="quote_div">丢开有效性检验不说,这个代码绕的弯子太大了。算天数只要一代码:<br>$day=floor((time()-strtotime($date))/86400);<br>确实,strtotime是可以的,多谢指点。:-)<br>而且,strtotime支持的日期格式很丰富,学习了。<br>http://www.php.net/manual/en/datetime.formats.date.php <div class="clear"> </div> </div> </div></div> </div></div> </div></div> </div> </div> </div></div> </div></div> </div> </div> </div></div> </div> </div> </div>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11?

OOBELANGUAGE Error Problems in Windows 11/10 Repair

How to enable or disable taskbar thumbnail previews on Windows 11

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari?
