Php custom format time sample code

Source: Internet
Author: User

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;

}

 

 

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.