You are here

public function jDate::ago in Persian Date for Drupal 8 8.4

Return value

string

1 call to jDate::ago()
jDate::until in src/Library/Jalali/jDate.php

File

src/Library/Jalali/jDate.php, line 103

Class

jDate
Class jDate @package Morilog\Jalali

Namespace

Drupal\persian_date\Library\Jalali

Code

public function ago() {
  $now = time();
  $time = $this
    ->getDateTime()
    ->getTimestamp();

  // catch error
  if (!$time) {
    return false;
  }

  // build period and length arrays
  $periods = array(
    'ثانیه',
    'دقیقه',
    'ساعت',
    'روز',
    'هفته',
    'ماه',
    'سال',
    'قرن',
  );
  $lengths = array(
    60,
    60,
    24,
    7,
    4.35,
    12,
    10,
  );

  // get difference
  $difference = $now - $time;

  // set descriptor
  if ($difference < 0) {
    $difference = abs($difference);

    // absolute value
    $negative = true;
  }

  // do math
  for ($j = 0; $difference >= $lengths[$j] and $j < count($lengths) - 1; $j++) {
    $difference /= $lengths[$j];
  }

  // round difference
  $difference = intval(round($difference));

  // return
  return number_format($difference) . ' ' . $periods[$j] . ' ' . (isset($negative) ? '' : 'پیش');
}