Home > Backend Development > PHP Tutorial > php输出指定时间以前时间格式的方法_php技巧

php输出指定时间以前时间格式的方法_php技巧

WBOY
Release: 2016-05-16 20:19:48
Original
1087 people have browsed it

本文实例讲述了php输出指定时间以前时间格式的方法。分享给大家供大家参考。具体分析如下:

比如说你需要在php中输出3天前,20分钟以前,可以参考下面的代码

function ago($time) {
 $time = strtotime($time);
 $delta = time() - $time;
 if ($delta < 60) {
  return 'less than a minute ago.';
 } else if ($delta < 120) {
  return 'about a minute ago.';
 } else if ($delta < (45 * 60)) {
  return floor($delta / 60) . ' minutes ago.';
 } else if ($delta < (90 * 60)) {
  return 'about an hour ago.';
 } else if ($delta < (24 * 60 * 60)) {
  return 'about ' . floor($delta / 3600) . ' hour(s) ago.';
 } else if ($delta < (48 * 60 * 60)) {
  return '1 day ago.';
 } else {
  return floor($delta / 86400) . ' days ago.';
 }
}
Copy after login

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template