PHP converts a date string to the current number of days.

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

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.