Php instance code: time-friendly formatting _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php instance code: time-friendly formatting. Php instance code: time-friendly formatting. please refer to the code: CopytoClipboard reference: [www.veryhuo.com]? PhpclassDateFormat {privatestatic $ _ DIFF_FORMATarray (DAY php instance code: time-friendly formatting, please refer to the code:

Copy to ClipboardReference: [www.bkjia.com]
Class DateFormat
{
Private static $ _ DIFF_FORMAT = array (
'Day' => '% s days ago ',
'Day _ hour' => '% s DAY % s HOUR before ',
'Hour '=>' % s HOUR ',
'Hour _ MINUTE '=>' % s HOUR % s before ',
'Minute '=>' % s Minutes Ago ',
'Minute _ second' => '% s minutes % s before ',
'Second' => '% s Minutes Ago ',
);

/**
* Friendly formatting time
*
* @ Param int time
* @ Param array $ formats
* @ Return string
*/
Public static function diff ($ timestamp, $ formats = null)
{
If ($ formats = null ){
$ Formats = self: $ _ DIFF_FORMAT;
}
/* Calculate the time difference */
$ Seconds = time ()-$ timestamp;
$ Minutes = floor ($ seconds/60 );
$ Hours = floor ($ minutes/60 );
$ Days = floor ($ hours/24 );

If ($ days> 0 ){
$ DiffFormat = 'day ';
} Else {
$ DiffFormat = ($ hours> 0 )? 'Hour': 'Minute ';
If ($ diffFormat = 'hour '){
$ DiffFormat. = ($ minutes> 0 & ($ minutes-$ hours * 60)> 0 )? '_ MINUTE ':'';
} Else {
$ DiffFormat = ($ seconds-$ minutes * 60)> 0 & $ minutes> 0)
? $ DiffFormat. '_ second': 'second ';
}
}

$ DateDiff = null;
Switch ($ diffFormat ){
Case 'day ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ days );
Break;
Case 'Day _ hour ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ days, $ hours-$ days * 60 );
Break;
Case 'hour ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ hours );
Break;
Case 'hour _ MINUTE ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ hours, $ minutes-$ hours * 60 );
Break;
Case 'Minute ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ minutes );
Break;
Case 'minute _ second ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ minutes, $ seconds-$ minutes * 60 );
Break;
Case 'second ':
$ DateDiff = sprintf ($ formats [$ diffFormat], $ seconds );
Break;
}
Return $ dateDiff;
}
}

Echo DateFormat: diff ('20140901 ');
/* 33 minutes 47 seconds ago */

[Www.veryhuo.com]? Php class DateFormat {private static $ _ DIFF_FORMAT = array ('day '...

Related Article

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.