Static php time conversion method-PHP source code

Source: Internet
Author: User
Ec (2); php static time conversion method ** & nbsp; * Static time Conversion Method & nbsp; * @ paramstring $ formatTo return format & nbsp; * @ parammixed $ source TIME source & nbsp; * @ returnstring & nbsp; * & nbsp; publicstaticfunctiontransform ($ fo script ec (2); script

Static Method of php time Conversion

/**
* Static time Conversion Method
* @ Param string $ formatTo return format
* @ Param mixed $ source TIME source
* @ Return string
*/
Public static function transform ($ formatTo = 'date', $ source ){
If (! Is_numeric ($ source )){
Try {
$ Source = MyDate: toTimeStamp ($ source );
} Catch (Exception $ e ){

}
}
If ($ formatTo! = 'Timestamp '){
Switch (strtolower ($ formatTo ))
{
Case 'China': // chinese Format String "YYYY-MM-DD"

HH: MM: SS"
$ Result = date ("Y, m, D, H: I: s ",

$ Source); break;
Case 'date ':
$ Result = date ("Y, m, D ",

$ Source); break;
Case 'date ':
// $ Result = $ source; break;
$ Result = date ("Y-m-d", $ source );

Break;
Case 'time ':
$ Result = date ("H: I: s", $ source );

Break;
Case 'normal': // standard Format String "YYYY-MM-DD

HH: MM: SS"
$ Result = date ("Y-m-d H: I: s ",

$ Source); break;
Case 'noyear ':
$ Result = date ("m-d", $ source); break;
Default:
$ Result = date ($ formatTo, $ source );
}
} Else {
$ Result = $ source;
}
Return $ result;
}

/**
* Time subtraction
* @ Param string $ source original time
* @ Param string $ dest target time
* @ Param string $ unit
* @ Param bool $ roundIt: whether to round the result
* @ Return int
*/
Public static function compare ($ source, $ dest, $ unit, $ roundIt =

False ){
If ($ source & $ dest)
{
If (is_object ($ source) & get_class ($ source)

= 'Mydate '){
$ Source = $ source-> getTimeStamp ();
}
If (is_object ($ dest) & get_class ($ dest) = 'mydate ')

{
$ Dest = $ dest-> getTimeStamp ();
}
If (! Is_numeric ($ source )){
$ Source = MyDate: toTimeStamp ($ source );
}
If (! Is_numeric ($ dest )){
$ Dest = MyDate: toTimeStamp ($ dest );
}
$ Result = $ source-$ dest;
If ($ result! = 0 ){
Switch ($ unit ){
Case 'yy': $ result =

$ Result/31536000; break; // year
Case 'mm': $ result =

$ Result/2592000; break; // month
Case 'dd': $ result = $ result/86400;

Break; // day
Case 'H': $ result = $ result/3600;

Break; // hour
Case 'M': $ result = $ result/60;

Break; // minute
Default: break;

// Second
}
}
If ($ roundIt ){
$ Result = intval (round ($ result ));
}
} Else {
$ Result = false;
}
Return $ result;
}

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.