Php: Custom formatting Time Method _ PHP Tutorial

Source: Internet
Author: User
Php: Custom formatting time method. For example, if the time is five minutes ago, the corresponding timestamp will be converted to 5 minutes ago. if you don't talk about it, paste the code directly: * ** formatting time * @ paraminteger $ timestamp. for example, if the time is just five minutes ago, the corresponding timestamp will be formatted as 5 minutes ago, directly paste the code:

/*** Format 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', 'day' => 'Yesterday ', 'byday' => 'day', 'hour' => 'hour ', 'Half' => 'Half ', 'min' => 'min', 'SEC' => 'second', '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> 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 ;}


WaterMark/*** formatting time * @ param integer $ timestamp...

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.