php strtotime反向

前端之家收集整理的这篇文章主要介绍了php strtotime反向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP中是否有一些函数timetostr将在今天/明天/下一个星期日/等输出.从给定的时间戳?所以timetostr(strtotime(x))= x
这可能对来这里的人有用.
  1. /**
  2. * Format a timestamp to display its age (5 days ago,in 3 days,etc.).
  3. *
  4. * @param int $timestamp
  5. * @param int $now
  6. * @return string
  7. */
  8. function timetostr($timestamp,$now = null) {
  9. $age = ($now ?: time()) - $timestamp;
  10. $future = ($age < 0);
  11. $age = abs($age);
  12.  
  13. $age = (int)($age / 60); // minutes ago
  14. if ($age == 0) return $future ? "momentarily" : "just now";
  15.  
  16. $scales = [
  17. ["minute","minutes",60],["hour","hours",24],["day","days",7],["week","weeks",4.348214286],// average with leap year every 4 years
  18. ["month","months",12],["year","years",10],["decade","decades",["century","centuries",1000],["millenium","millenia",@R_301_461@_INT_MAX]
  19. ];
  20.  
  21. foreach ($scales as list($singular,$plural,$factor)) {
  22. if ($age == 0)
  23. return $future
  24. ? "in less than 1 $singular"
  25. : "less than 1 $singular ago";
  26. if ($age == 1)
  27. return $future
  28. ? "in 1 $singular"
  29. : "1 $singular ago";
  30. if ($age < $factor)
  31. return $future
  32. ? "in $age $plural"
  33. : "$age $plural ago";
  34. $age = (int)($age / $factor);
  35. }
  36. }

猜你在找的PHP相关文章