PHP - epoch to time ago (function)

0 votes
542 views
added Mar 27, 2019 in PHP by LC Marshal Captain (25,790 points)
<?php 
  // do not include this function (or any function) into loop, it'll not work.
  function time_elapsed_string($datetime, $full = false) {
      $now = new DateTime;
      $ago = new DateTime($datetime);
      $diff = $now->diff($ago);

      $diff->w = floor($diff->d / 7);
      $diff->d -= $diff->w * 7;

      $string = array(
          'y' => 'year',
          'm' => 'month',
          'w' => 'week',
          'd' => 'day',
          'h' => 'hour',
          'i' => 'minute',
          's' => 'second',
      );
      foreach ($string as $k => &$v) {
          if ($diff->$k) {
              $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
          } else {
              unset($string[$k]);
          }
      }

      if (!$full) $string = array_slice($string, 0, 1);
      return $string ? implode(', ', $string) . ' ago' : 'just now';
  }
?>




<?php foreach($results as $row) : ?>
<div class="row no-gutters single-card">
  <div class="col-md-7 col-7">
    <h4 class="news-title"><a href="/<?php print $row->path; ?>" target="_blank"><?php print $row->node_title; ?></a></h4>
    <?php
        // "node_created":"1553498100" get value from API
        $newsdate = $row->node_created;
        $timeAgo = date("F j, Y", substr($newsdate, 0, 10));
    ?>
    <!--use the output by print the function of variable -->
    <p class="news-date"><?php print time_elapsed_string($timeAgo); ?></p>
  </div>
</div>
<?php endforeach; ?>

<!--out put of function: 2 days ago -->

 

1 Response

0 votes
responded Mar 12, 2020 by LC Marshal Captain (25,790 points)
<?php
  function timeago($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
  }
  function generateTimeAgo($tCreated, $tChanged) {
    $strTimeAgo = '';
    // date created
    if(!empty($tCreated)) {
      $t = gmdate("Y-m-d\TH:i:s\Z", $tCreated);
      $strTimeAgo = timeago($t);
      // use date changed if exists
      if(!empty($tChanged)) {
        $t = gmdate("Y-m-d\TH:i:s\Z", $tChanged);
        $strTimeAgo = 'Updated ' . timeago($t);
      }
    }
    return $strTimeAgo;
  }

?>

 

Usage:

<span class="date-published">
  <?php echo date("F d, Y", strtotime($editorPick->publishdate));?>
  <?php echo ' | ' . generateTimeAgo($editorPick->timestamp_created, $editorPick->publishdate_react); ?>
</span>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...