PHP converts a date string to the current number of days as an example and input a date string. for example, 2011-3-23 outputs the current number of days as an example. for example: 1? Code :? PublicstaticfunctionconvertDateToLong ($ d PHP converts a date string to an example of the current number of days
Enter a date string, for example, 2011-3-23.
The output is the current number of days, for example, 1.
?
Code:
?
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!!!"); } }
On the first floor, bardo left the validity test blank. the code is too curved. Only one code is required for calculating the number of days:
$ Day = floor (time ()-strtotime ($ date)/86400); on the second floor, liuzhiqiangruc bardo wrote that the validity test is not mentioned, and the code is too curved. Only one code is required for calculating the number of days:
$ Day = floor (time ()-strtotime ($ date)/86400 );
Indeed, strtotime is fine. thank you for your advice. :-) On the third floor, liuzhiqiangruc 2011-03-30 liuzhiqiangruc wrote that bardo wrote that the code was too large to bypass the validity test. Only one code is required for calculating the number of days:
$ Day = floor (time ()-strtotime ($ date)/86400 );
Indeed, strtotime is fine. thank you for your advice. :-)
In addition, strtotime supports a wide range of date formats. 4 floor liuzhiqiangruc 2011-03-30 liuzhiqiangruc wrote bardo wrote that the effectiveness test is not to mention, this code around the bend is too big. Only one code is required for calculating the number of days:
$ Day = floor (time ()-strtotime ($ date)/86400 );
Indeed, strtotime is fine. thank you for your advice. :-)
In addition, strtotime supports a wide range of date formats.
Http://www.php.net/manual/en/datetime.formats.date.php