PHP converts a date string to an example of the current number of days
Enter as a date string, for example: 2011-3-23
The output is an example of the current number of days, for example: 1
?
The code is:
?
public static function Convertdatetolong ($DATESTR) {$checkPattern = "/^\d{4} (((-\d {.}) {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 is 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!!!"); } }
1/F Bardo 2011-03-26
Leave the validity test aside, this code bend too big. Count days as long as one code:
$day =floor ((Time ()-strtotime ($date))/86400);
2/F Liuzhiqiangruc 2011-03-30
Bardo wrote
Leave the validity test aside, this code bend too big. Count days as long as one code:
$day =floor ((Time ()-strtotime ($date))/86400);
Indeed, Strtotime is possible, thank you for your advice. :-)
3/F Liuzhiqiangruc 2011-03-30
Liuzhiqiangruc wrote
Bardo wrote
Leave the validity test aside, this code bend too big. Count days as long as one code:
$day =floor ((Time ()-strtotime ($date))/86400);
Indeed, Strtotime is possible, thank you for your advice. :-)
Furthermore, the Strtotime supported date format is rich and learning.
4/F Liuzhiqiangruc 2011-03-30
Liuzhiqiangruc wrote
Liuzhiqiangruc wrote
Bardo wrote
Leave the validity test aside, this code bend too big. Count days as long as one code:
$day =floor ((Time ()-strtotime ($date))/86400);
Indeed, Strtotime is possible, thank you for your advice. :-)
Furthermore, the Strtotime supported date format is rich and learning.
http://www.php.net/manual/en/datetime.formats.date.php