Php custom formatting Time example code _ PHP Tutorial

Source: Internet
Author: User
Php custom format time sample code. For example, if the time is 5 minutes ago, the corresponding timestamp will be formatted as 5 minutes ago. if you don't need to talk about it, paste the code directly: Copy the code as follows: * ** format Time * @ par. for example, if the time is five minutes ago, the corresponding timestamp will be formatted as five minutes ago. if you don't need to talk about it, paste the code directly:

The code is as follows:


/**
* Formatting time
* @ Param integer $ timestamp
* @ Param string $ format dt = date and time d = date t = time u = personalized others = custom
* @ Param integer $ timeoffset time zone value
* @ Param string $ custom_format custom time format
* @ Return string
*/
Public function dgmdate ($ timestamp, $ format = 'dt', $ timeoffset = '000000', $ custom_format = ''){
$ Return = '';
$ Now = time ();
$ Day_format = 'Y-n-j ';
$ Time_format = 'H: I: s ';
$ Date_format = $ day_format. ''. $ time_format;
$ Offset = 8; // The default value is UTC + 8, that is, Beijing time.
$ Lang = array (
'Before' => 'pre ',
'Day' => 'day ',
'Yday' => 'Yesterday ',
'Byday' => 'day before Day ',
'Hour '=> 'hour ',
'Half' => 'Half ',
'Min' => 'min ',
'SEC '=> 'SEC ',
'Now '=> 'gang ',
);
$ Timeoffset = 9999? $ Offset: $ timeoffset;
$ Timestamp + = $ timeoffset * 3600;
Switch ($ format ){
Case 'dt ':
$ Format = $ date_format;
Break;
Case 'D ':
$ Format = $ day_format;
Break;
Case 'T ':
$ Format = $ time_format;
Break;
}
If ($ format = 'u '){
$ Todaytimestamp = $ now-($ now + $ timeoffset * 3600) % 86400 + $ timeoffset * 3600;
$ S = gmdate (empty ($ custom_format )? $ Date_format: $ custom_format, $ timestamp );
$ Time = $ now + $ timeoffset * 3600-$ timestamp;
If ($ timestamp >=$ todaytimestamp ){
If ($ time & gt; 3600 ){
$ Return = ''. intval ($ time/3600). $ lang ['hour']. $ lang ['before'].'';
} Elseif ($ time> 1800 ){
$ Return = ''. $ lang ['half']. $ lang ['hour']. $ lang ['before'].'';
} Elseif ($ time> 60 ){
$ Return = ''. intval ($ time/60). $ lang ['min']. $ lang ['before'].'';
} Elseif ($ time> 0 ){
$ Return = ''. $ time. $ lang ['SEC ']. $ lang ['before'].'';
} Elseif ($ time = 0 ){
$ Return = ''. $ lang ['right'].'';
} Else {
$ Return = $ s;
}
} Elseif ($ days = intval ($ todaytimestamp-$ timestamp)/86400) >=0 & $ days <7 ){
If ($ days = 0 ){
$ Return = ''. $ lang ['yday']. gmdate ($ time_format, $ timestamp ).'';
} Elseif ($ days = 1 ){
$ Return = ''. $ lang ['byday']. gmdate ($ time_format, $ timestamp ).'';
} Else {
$ Return = ''. ($ days + 1). $ lang ['day']. $ lang ['before'].'';
}
} Else {
$ Return = $ s;
}
} Else {
$ Return = gmdate ($ format, $ timestamp );
}
Return $ return;
}

The format code is as follows:/*** format Time ** @ par...

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.