date / time convert to time since php

后端 未结 5 710
你的背包
你的背包 2021-01-14 10:14

Hello in my database date / time are in this format

2010-06-01T18:20:25+0000

I\'d like to echo that out to time passed since that date / t

5条回答
  •  没有蜡笔的小新
    2021-01-14 10:49

    function time_ago($timestamp, $granularity = 2) {
      $timestamp = time() - $timestamp;
      $units = array('1 year|%d years' => 31536000, 
                     '1 week|%d weeks' => 604800, 
                     '1 day|%d days' => 86400, 
                     '1 hour|%d hours' => 3600, 
                     '1 min|%d mins' => 60, 
                     '1 sec|%d secs' => 1
                    );
      $output = '';
      foreach ($units as $key => $value) {
        $key = explode('|', $key);
        if ($timestamp >= $value) {
          $pluralized = floor($timestamp / $value) > 1 ? 
                        sprintf($key[1], floor($timestamp / $value)) : 
                        $key[0];
          $output .= ($output ? ' ' : '') . $pluralized;
          $timestamp %= $value;
          $granularity--;
        }
        if ($granularity == 0) {
          break;
        }
      }
      return $output ? $output : "Just now";
    }
    

    This should be close.

    Edit: added this line: $timestamp = time() - $timestamp;

提交回复
热议问题