$_SESSION['WAREHOUSE']['theme'] ]; /* [3] Store functions =========================================================*/ $twig->addFunction(new \Twig_Function('f_history', function(){ $req = new Request('historyDefault/getAll', []); $res = $req->dispatch(); // si erreur, on retourne rien par défaut if( $res->error->get() != Err::Success ) return []; return $res->get('history'); })); $twig->addFilter(new \Twig_Filter('f_tsformat', function($ts){ date_default_timezone_set('Europe/Paris'); return date('d/m/Y H:i:s', $ts); })); $twig->addFilter(new \Twig_Filter('f_tsrelative', function($ts){ $r = self::relativetime($ts); // if only one $o = $r[0] <= 1; switch($r[1]){ case 'Y': return $r[0].' an'.($o?'':'s'); break; case 'm': return $r[0].' mois'; break; case 'd': return $r[0].' jour'.($o?'':'s'); break; case 'H': return $r[0].' heure'.($o?'':'s'); break; case 'i': return $r[0].' minute'.($o?'':'s'); break; case 's': return $r[0].' seconde'.($o?'':'s'); break; default: return 'peu de temps'; break; } })); /* [4] Build the whole stuff =========================================================*/ return $twig->render('history/view.twig', [ 'p_theme' => $variables['p_theme'] ]); } /* RETURNS A RELATIVE HUMAN-READABLE TIME * * @ts Timestamp to process * * @return relative human-readable relative time [value, unit] * */ private static function relativetime($ts){ /* [1] Explode time into human-readable time units =========================================================*/ $units = []; /* (1) Date units */ $units['year'] = (int) date('Y', $ts); $units['month'] = (int) date('m', $ts); $units['day'] = (int) date('d', $ts); /* (2) Time units */ $units['hour'] = (int) date('H', $ts); $units['minute'] = (int) date('i', $ts); $units['second'] = (int) date('s', $ts); /* [2] Calculate relative time for each unit =========================================================*/ /* (1) Date units */ $units['year'] = intval(date('Y')) - $units['year']; $units['month'] = intval(date('m')) - $units['month']; $units['day'] = intval(date('d')) - $units['day']; /* (2) Time units */ $units['hour'] = intval(date('H')) - $units['hour']; $units['minute'] = intval(date('i')) - $units['minute']; $units['second'] = intval(date('s')) - $units['second']; /* [3] Return significative relative time =========================================================*/ /* (1) Date units */ if( $units['year'] > 0 ) return [ $units['year'], 'Y' ]; if( $units['month'] > 0 ) return [ $units['month'], 'm' ]; if( $units['day'] > 0 ) return [ $units['day'], 'd' ]; /* (2) Time units */ if( $units['hour'] > 0 ) return [ $units['hour'], 'H' ]; if( $units['minute'] > 0 ) return [ $units['minute'], 'i' ]; if( $units['second'] > 0 ) return [ $units['second'], 's' ]; /* (3) Default value */ return [0, '.']; } }