Convert the timestamp stored in the database to the past time format

Source: Internet
Author: User
: This article describes how to convert the timestamp stored in the database to the previous time format. For more information about PHP tutorials, see. This function is generally used to display posts published on Weibo and blogs. For example, if a Weibo post is published on Sina Weibo today, it will display the release in XX seconds, XX hours ago, rather than the specific time. If you forward a previously published Weibo post, the original Weibo post will display its release time.
So how to implement it? The code is as follows:

/** Format time function * @ param $ time the timestamp to be formatted */functiontime_format ($ time) {$ now = time (); $ tody = strtotime (date ('Y-m-D'); $ diff = $ now-$ time; $ str = ''; switch ($ time) {case $ diff <60: $ str = $ diff. 'seconds ago '; break; case $ diff <3600: $ str = floor ($ diff/60 ). 'minute ago '; break; case $ diff <(3600*8): $ str = floor ($ diff/3600 ). 'hour ago '; break; case $ time> $ tody: $ str = 'Today '. date ('Y-m-d H: I: S', $ time); break; default: $ str = date ('Y-m-d H: I: s ', $ time) ;}return $ str ;}$ time = 1423110837; // simulate the timestamp echo time_format ($ time) saved in the database );

Explanation:
1. First, we need to obtain the current time: Now = time (); 2. we need to obtain the timestamp of 00:00:00 today to compare the time in the database to today or yesterday. That is:

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.