: 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: