jDate.php in Persian Date for Drupal 8 8.4
File
src/Library/Jalali/jDate.php
View source
<?php
namespace Drupal\persian_date\Library\Jalali;
class jDate {
protected $dateTime;
protected $formats = array(
'datetime' => '%Y-%m-%d %H:%M:%S',
'date' => '%Y-%m-%d',
'time' => '%H:%M:%S',
);
public static function forge($str = null, $timezone = null) {
return new static($str, $timezone);
}
public function __construct($str = null, $timezone = null) {
$this->dateTime = jDateTime::createDateTime($str, $timezone);
}
public function getDateTime() {
return $this->dateTime;
}
public function format($format) {
if (in_array($format, array_keys($this->formats))) {
$format = $this->formats[$format];
}
if ($this->dateTime !== false) {
return jDateTime::strftime($format, $this->dateTime
->getTimestamp(), $this->dateTime
->getTimezone());
}
else {
return false;
}
}
public function reforge($str) {
$this->dateTime
->modify($str);
return $this;
}
public function ago() {
$now = time();
$time = $this
->getDateTime()
->getTimestamp();
if (!$time) {
return false;
}
$periods = array(
'ثانیه',
'دقیقه',
'ساعت',
'روز',
'هفته',
'ماه',
'سال',
'قرن',
);
$lengths = array(
60,
60,
24,
7,
4.35,
12,
10,
);
$difference = $now - $time;
if ($difference < 0) {
$difference = abs($difference);
$negative = true;
}
for ($j = 0; $difference >= $lengths[$j] and $j < count($lengths) - 1; $j++) {
$difference /= $lengths[$j];
}
$difference = intval(round($difference));
return number_format($difference) . ' ' . $periods[$j] . ' ' . (isset($negative) ? '' : 'پیش');
}
public function until() {
return $this
->ago();
}
public function time() {
return $this->dateTime
->getTimestamp();
}
}
Classes
Name |
Description |
jDate |
Class jDate
@package Morilog\Jalali |