/** * twig custom filter for convert date to best type of showing on each language * tdate means translated date */ public function twig_filter_tdate() { return new \Twig_SimpleFilter('tdate', function ($_string, $_format = "Y/m/d", $_convert = true) { $result = $_string; if ($this->data->site['currentlang'] == 'fa') { $result = \lib\utility\jdate::date($_format, $_string, $_convert); } else { $result = date($_format, strtotime($_string)); } return $result; }); }
/** * check saloos language and if needed convert to persian date * else show default date * @param [type] $_date [description] * @return [type] [description] */ public static function date($_format, $_stamp = false, $_type = false, $_persianChar = true) { $result = null; if (strlen($_stamp) < 2) { $_stamp = false; } // get target language if ($_type === 'default') { $_type = substr(\lib\router::get_storage('defaultLanguage'), 0, 2); } elseif ($_type === 'current') { $_type = substr(\lib\router::get_storage('language'), 0, 2); } // if need persian use it else use default date function if ($_type === true || $_type === 'fa') { $result = \lib\utility\jdate::date($_format, $_stamp, $_persianChar); } else { if ($_stamp) { $result = date($_format, $_stamp); } else { $result = date($_format); } } return $result; }
public static function humanTiming($_time, $_max = 'ultimate', $_format = "Y/m/d", $_lang = 'en') { // auto convert with strtotime function $_time = strtotime($_time); $time_diff = time() - $_time; // to get the time since that moment $tokens = array(31536000 => T_('year'), 2592000 => T_('month'), 604800 => T_('week'), 86400 => T_('day'), 3600 => T_('hour'), 60 => T_('minute'), 1 => T_('second')); if ($time_diff < 10) { return T_('A few seconds ago'); } $_max = array_search(T_($_max), $tokens); foreach ($tokens as $unit => $text) { if ($time_diff < $unit) { continue; } // if time diff less than user request change it to humansizing if ($time_diff < $_max || $_max == T_('ultimate')) { $numberOfUnits = floor($time_diff / $unit); return $numberOfUnits . ' ' . $text . ($numberOfUnits > 1 ? T_('s ') : ' ') . T_('ago'); } else { if ($_lang == 'fa') { return \lib\utility\jdate::date($_format, $_time); } else { return date($_format, $_time); } } } }
/** * twig custom filter for convert date to jalai with custom format like php date func format */ public function twig_filter_jdate() { return new \Twig_SimpleFilter('jdate', function ($_string, $_format = "Y/m/d") { return \lib\utility\jdate::date($_format, $_string); }); }