Php custom format time sample code
The time is exactly 5 minutes ago, and the corresponding timestamp will be formatted as 5 minutes ago. The custom formatting method is as follows. If you are interested, refer
For example, if the time is 5 minutes ago, the corresponding timestamp will be formatted as 5 minutes ago. If you don't 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 = '<span title = "'. $ s. '"> '. intval ($ time/3600 ). $ lang ['hour']. $ lang ['before']. '</span> ';
} Elseif ($ time> 1800 ){
$ Return = '<span title = "'. $ s. '"> '. $ lang ['half']. $ lang ['hour']. $ lang ['before']. '</span> ';
} Elseif ($ time> 60 ){
$ Return = '<span title = "'. $ s. '"> '. intval ($ time/60 ). $ lang ['Min']. $ lang ['before']. '</span> ';
} Elseif ($ time> 0 ){
$ Return = '<span title = "'. $ s. '"> '. $ time. $ lang ['sec ']. $ lang ['before']. '</span> ';
} Elseif ($ time = 0 ){
$ Return = '<span title = "'. $ s. '">'. $ lang ['right']. '</span> ';
} Else {
$ Return = $ s;
}
} Elseif ($ days = intval ($ todaytimestamp-$ timestamp)/86400) >=0 & $ days <7 ){
If ($ days = 0 ){
$ Return = '<span title = "'. $ s. '"> '. $ lang ['yday']. gmdate ($ time_format, $ timestamp ). '</span> ';
} Elseif ($ days = 1 ){
$ Return = '<span title = "'. $ s. '"> '. $ lang ['byday']. gmdate ($ time_format, $ timestamp ). '</span> ';
} Else {
$ Return = '<span title = "'. $ s. '"> '. ($ days + 1 ). $ lang ['day']. $ lang ['before']. '</span> ';
}
} Else {
$ Return = $ s;
}
} Else {
$ Return = gmdate ($ format, $ timestamp );
}
Return $ return;
}