Php format timestamp, php format

Source: Internet
Author: User

Php format timestamp, php format

In CMS, it usually takes a few minutes to display a new article with a relatively new display time. A few days ago, this is the case, but generally the database records all timestamps (at least I used to do this ), therefore, we need a conversion process. Based on the information on the Internet, we have added our own modifications and encapsulated two sections of code.

The following is an encapsulated method.

Function formatTime ($ time) {$ time = (int) substr ($ time, 0, 10); $ int = time ()-$ time; $ str = ''; if ($ int <= 2) {$ str = sprintf ('hangzhou', $ int);} elseif ($ int <60) {$ str = sprintf ('% d seconds ago', $ int);} elseif ($ int <3600) {$ str = sprintf ('% d minutes ago ', floor ($ int/60);} elseif ($ int <86400) {$ str = sprintf ('% d h ago', floor ($ int/3600 ));} elseif ($ int <2592000) {$ str = sprintf ('% d days ago', floor ($ int/86400 ));} else {$ str = date ('Y-m-d H: I: s', $ time);} return $ str ;}

Or more detailed

Function formatTime ($ time) {$ text = ''; $ time = intval ($ time); $ ctime = time (); $ t = $ ctime-$ time; // time difference if ($ t <0) {return date ('Y-m-d', $ time) ;}; $ Y = date ('y', $ ctime) -date ('y', $ time); // whether to switch ($ t) {case $ t = 0: $ text = 'new'; break; case $ t <60: // $ text = $ t in one minute. 'Seconds ago '; break; case $ t <3600: // $ text = floor ($ t/60) within an hour ). 'minute ago '; break; case $ t <86400: // $ text = floor ($ t/3600) in a day ). 'hour ago '; // break in a day; case $ t <2592000: // if ($ time> strtotime (date ('ymmd ', strtotime ("-1 day") {$ text = 'Yesterday ';} elseif ($ time> strtotime (date ('ymd ', strtotime ("-2 days") {$ text = 'day before yesterday ';} else {$ text = floor ($ t/86400 ). 'Day before ';} break; case $ t <31536000 & $ y = 0: // not cross-year in a year $ m = date ('M', $ ctime) -date ('m', $ time)-1; if ($ m = 0) {$ text = floor ($ t/86400 ). 'day';} else {$ text = $ m. 'months ago ';} break; case $ t <31536000 & $ y> 0: // cross-year in a year $ text = (11-date ('M ', $ time) + date ('M', $ ctime )). 'months ago '; break; default: $ text = (date ('y', $ ctime)-date ('y', $ time )). 'years ago '; break;} return $ text ;}

Share another similar

// Format the friendly display time function formatTime ($ time) {$ now = time (); $ day = date ('Y-m-d', $ time ); $ today = date ('Y-m-d'); $ dayArr = explode ('-', $ day); $ todayArr = explode ('-', $ today ); // The number of days in the distance. This method is not necessarily accurate if it exceeds 30 days, but it is accurate within 30 days, because a month may be 30 days or 31 days $ days = ($ todayArr [0]-$ dayArr [0]) * 365 + ($ todayArr [1]-$ dayArr [1]) * 30) + ($ todayArr [2]-$ dayArr [2]); // The number of seconds from the distance $ secs = $ now-$ time; if ($ todayArr [0]-$ dayArr [0]> 0 & $ days> 3) {// return date ('Y-m-d', $ time) beyond 3 days across the year;} else {if ($ days <1) {// Today if ($ secs <60) return $ secs. 'Seconds ago '; elseif ($ secs <3600) return floor ($ secs/60 ). "Minutes Ago"; else return floor ($ secs/3600 ). "Hours Ago";} else if ($ days <2) {// Yesterday $ hour = date ('h', $ time); return "yesterday ". $ hour. 'point';} elseif ($ days <3) {// the day before yesterday $ hour = date ('h', $ time); return "the day before yesterday ". $ hour. 'point';} else {// return date ('m month d ', $ time) Three days ago );}}}

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.