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 ($ dateStr) {$ checkPattern ^ d {4} (-d {1, 2}) {2}) | ((. 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!!!"); } }
Bardo on the first floor
If you leave 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 );
2 floor liuzhiqiangruc
Bardo wrote:
If you leave 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 );
Indeed, strtotime is fine. thank you for your advice. :-)
3rd floor liuzhiqiangruc
Liuzhiqiangruc wrote
Bardo wrote:
If you leave 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 );
Indeed, strtotime is fine. thank you for your advice. :-)
In addition, strtotime supports a wide range of date formats.
4 floor liuzhiqiangruc
Liuzhiqiangruc wrote
Liuzhiqiangruc wrote
Bardo wrote:
If you leave 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 );
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