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...