PHP displays time as 'just', 'n minutes/hours ago', etc.
Release: 2016-07-25 08:43:12
Original
1052 people have browsed it
In many occasions, in order to show the timeliness of information, the time is usually displayed as "just now", "5 minutes ago", "3 hours ago", etc. instead of printing the time directly. For example, Weibo and SNS applications have long used this function. The time format generally stored in the database is Unix timestamp, so here is a PHP function that converts Unix timestamp into timeline display.
-
- date_default_timezone_set('PRC');
- $date = "1351836000";
- echo tranTime($date);
-
- function transfer_time($time)
- {
- $rtime = date(" m-d H:i",$time);
- $htime = date("H:i",$time);
-
- $time = time() - $time;
-
- if ($time < 60)
- {
- $str = 'just';
- }
- elseif ($time < 60 * 60)
- {
- $min = floor($time/60);
- $str = $min.'minutes ago';
- }
- elseif ($time < 60 * 60 * 24)
- {
- $h = floor($time/(60*60));
- $str = $h.'hours ago'.$htime;
- }
- elseif ( $time < 60 * 60 * 24 * 3)
- {
- $d = floor($time/(60*60*24));
- if($d==1)
- $str = 'yesterday'.$ rtime;
- else
- $str = 'the day before yesterday'.$rtime;
- }
- else
- {
- $str = $rtime;
- }
- return $str;
- }
-
- ?>
Copy code
|
PHP
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31