PHP converts a date string to an example of the current number of days

Source: Internet
Author: User
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.