src/Twig/AppExtension.php line 18

Open in your IDE?
  1. <?php 
  2. namespace App\Twig;
  3. use Twig\Extension\AbstractExtension;
  4. use Twig\TwigFilter;
  5. use Symfony\Component\HttpKernel\KernelInterface;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. class AppExtension extends AbstractExtension
  9. {
  10.     private $kernel;
  11.     private $router;
  12.     
  13.     public function __construct(KernelInterface $kernel,RouterInterface $router,ContainerInterface $container)
  14.     {
  15.         $this->kernel = $kernel;
  16.         $this->router = $router;
  17.         $this->container = $container;
  18.     }
  19.     
  20.     
  21.     public function getFilters()
  22.     {
  23.         return [
  24.             new TwigFilter('getTravelScheduleTimes', [$this, 'getTravelScheduleTimes']),
  25.             new TwigFilter('getTimeIntervalLogs', [$this, 'getTimeIntervalLogs']),
  26.             new TwigFilter('file_get_contents', [$this, 'fileGetContents']),
  27.             new TwigFilter('base64_encode', [$this, 'base64Encode']),           
  28.         ];
  29.     }
  30.     
  31.     public function fileGetContents($url)
  32.     {
  33.         return file_get_contents($url);
  34.     }
  35.     
  36.     public function base64Encode($string)
  37.     {
  38.         return base64_encode($string);
  39.     }
  40.     
  41.     public function getTimeIntervalLogs($second_date, $first_date){
  42.         if(strlen($first_date) > 0){
  43.             $fecha1 = new \DateTime($first_date); // Año-mes-día hora:minuto:segundo
  44.             $fecha2 = new \DateTime($second_date); // Año-mes-día hora:minuto:segundo
  45.             // Calcular la diferencia de tiempo
  46.             $diferencia = $fecha2->diff($fecha1);
  47.             
  48.             // Obtener la diferencia en días
  49.             $resultado = '';
  50.             
  51.             if ($diferencia->days > 0) {
  52.                 $resultado .= "$diferencia->days días, ";
  53.             }
  54.             if ($diferencia->h > 0) {
  55.                 $resultado .= $diferencia->h . ' horas, ';
  56.             }
  57.             if ($diferencia->i > 0) {
  58.                 $resultado .= $diferencia->i . ' minutos y ';
  59.             }
  60.             if ($diferencia->s > 0) {
  61.                 $resultado .= $diferencia->s . ' segundos';
  62.             }
  63.             
  64.             return $resultado;
  65.         }else{
  66.             return "";
  67.         }
  68.         
  69.     }
  70.     
  71.     public function getTravelScheduleTimes($start_at, $end_at){
  72.         if(strlen($start_at) > 0 && strlen($end_at) > 0 ){
  73.             $fechaInicio = new \DateTime($start_at);
  74.             $fechaFin = new \DateTime($end_at);
  75.             
  76.             $diferencia = $fechaInicio->diff($fechaFin);
  77.             
  78.             $dias = $diferencia->days;
  79.             $horas = $diferencia->h;
  80.             $minutos = $diferencia->i;
  81.             $segundos = $diferencia->s;
  82.             if($dias > 0){
  83.                 $data =  $dias . " Dias " . $horas . ":" . $minutos . ":" . $segundos;
  84.             }else{
  85.                 $data =  $horas . ":" . $minutos . ":" . $segundos;
  86.             }
  87.             return $data;
  88.             
  89.         }else{
  90.             return "-"; 
  91.         }
  92.         return ""; 
  93.         
  94.         
  95.     }
  96.     
  97.     
  98. }
  99. ?>