public function getTimeAgoFilter($value, $format = 'Y-m-d H:s')
 {
     if ($value == null) {
         return "";
     }
     // Devolvemos "" si es nulo
     if (!$value instanceof \DateTime) {
         $transformer = new DateTimeToStringTransformer(null, null, $format);
         $value = $transformer->reverseTransform($value);
     }
     return new TimeAgoHelper($value, null, $this->container);
 }
 /**
  * Reports the approximate distance in time between two times given in seconds
  * or in a valid ISO string like.
  * For example, if the distance is 47 minutes, it'll return
  * "about 1 hour". See the source for the complete wording list.
  *
  * Integers are interpreted as seconds. So, by example to check the distance of time between
  * a created user an it's last login:
  * {{ user.createdAt|distance_of_time_in_words(user.lastLoginAt) }} returns "less than a minute".
  *
  * Set include_seconds to true if you want more detailed approximations if distance < 1 minute
  *
  * @param $from_time String or DateTime
  * @param $to_time String or DateTime
  * @param bool $include_seconds
  *
  * @return mixed
  */
 public function distanceOfTimeInWordsFilter($from_time, $to_time = null, $include_seconds = false)
 {
     $datetime_transformer = new DateTimeToStringTransformer(null, null, 'Y-m-d H:i:s');
     $timestamp_transformer = new DateTimeToTimestampTransformer();
     # Transforming to Timestamp
     if (!$from_time instanceof \DateTime && !is_numeric($from_time)) {
         $from_time = $datetime_transformer->reverseTransform($from_time);
         $from_time = $timestamp_transformer->transform($from_time);
     } elseif ($from_time instanceof \DateTime) {
         $from_time = $timestamp_transformer->transform($from_time);
     }
     $to_time = empty($to_time) ? new \DateTime('now') : $to_time;
     # Transforming to Timestamp
     if (!$to_time instanceof \DateTime && !is_numeric($to_time)) {
         $to_time = $datetime_transformer->reverseTransform($to_time);
         $to_time = $timestamp_transformer->transform($to_time);
     } elseif ($to_time instanceof \DateTime) {
         $to_time = $timestamp_transformer->transform($to_time);
     }
     $distance_in_minutes = round(abs($to_time - $from_time) / 60);
     $distance_in_seconds = round(abs($to_time - $from_time));
     if ($distance_in_minutes <= 1) {
         if ($include_seconds) {
             if ($distance_in_seconds < 5) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 5));
             } elseif ($distance_in_seconds < 10) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 10));
             } elseif ($distance_in_seconds < 20) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 20));
             } elseif ($distance_in_seconds < 40) {
                 return $this->translator->trans('half a minute ago');
             } elseif ($distance_in_seconds < 60) {
                 return $this->translator->trans('less than a minute ago');
             } else {
                 return $this->translator->trans('1 minute ago');
             }
         }
         return $distance_in_minutes === 0 ? $this->translator->trans('less than a minute ago', array()) : $this->translator->trans('1 minute ago', array());
     } elseif ($distance_in_minutes <= 45) {
         return $this->translator->trans('%minutes minutes ago', array('%minutes' => $distance_in_minutes));
     } elseif ($distance_in_minutes <= 90) {
         return $this->translator->trans('about 1 hour ago');
     } elseif ($distance_in_minutes <= 1440) {
         return $this->translator->trans('about %hours hours ago', array('%hours' => round($distance_in_minutes / 60)));
     } elseif ($distance_in_minutes <= 2880) {
         return $this->translator->trans('1 day ago');
     } else {
         return $this->translator->trans('%days days ago', array('%days' => round($distance_in_minutes / 1440)));
     }
 }
 public function testReverseTransformWithNonExistingDate()
 {
     $reverseTransformer = new DateTimeToStringTransformer();
     $this->setExpectedException('Symfony\\Component\\Form\\Exception\\TransformationFailedException');
     $reverseTransformer->reverseTransform('2010-04-31');
 }
    public function testReverseTransformExpectsValidDateString()
    {
        $reverseTransformer = new DateTimeToStringTransformer();

        $this->setExpectedException('\InvalidArgumentException');

        $reverseTransformer->reverseTransform('2010-2010-2010', null);
    }
 /**
  * Reports the approximate distance in time between two times given in seconds
  * or in a valid ISO string like.
  * For example, if the distance is 47 minutes, it'll return
  * "about 1 hour". See the source for the complete wording list.
  *
  * Integers are interpreted as seconds. So, by example to check the distance of time between
  * a created user and their last login:
  * {{ user.createdAt|distance_of_time_in_words(user.lastLoginAt) }} returns "less than a minute".
  *
  * Set include_seconds to true if you want more detailed approximations if distance < 1 minute
  * Set include_months to true if you want approximations in months if days > 30
  *
  * @param string|\DateTime $from_time
  * @param string|\DateTime $to_time
  * @param bool             $include_seconds True to return distance in seconds when it's lower than a minute.
  * @param bool             $include_months
  *
  * @return string
  */
 public function distanceOfTimeInWordsFilter($from_time, $to_time = null, $include_seconds = false, $include_months = false)
 {
     $datetime_transformer = new DateTimeToStringTransformer(null, null, 'Y-m-d H:i:s');
     $timestamp_transformer = new DateTimeToTimestampTransformer();
     // Transform “from” to timestamp
     if ($from_time instanceof \DateTime) {
         $from_time = $timestamp_transformer->transform($from_time);
     } elseif (!is_numeric($from_time)) {
         $from_time = $datetime_transformer->reverseTransform($from_time);
         $from_time = $timestamp_transformer->transform($from_time);
     }
     $to_time = empty($to_time) ? new \DateTime('now') : $to_time;
     // Transform “to” to timestamp
     if ($to_time instanceof \DateTime) {
         $to_time = $timestamp_transformer->transform($to_time);
     } elseif (!is_numeric($to_time)) {
         $to_time = $datetime_transformer->reverseTransform($to_time);
         $to_time = $timestamp_transformer->transform($to_time);
     }
     $future = $to_time < $from_time;
     $distance_in_minutes = round(abs($to_time - $from_time) / 60);
     $distance_in_seconds = round(abs($to_time - $from_time));
     if ($future) {
         return $this->future($distance_in_minutes, $include_seconds, $distance_in_seconds);
     }
     if ($distance_in_minutes <= 1) {
         if ($include_seconds) {
             if ($distance_in_seconds < 5) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 5));
             }
             if ($distance_in_seconds < 10) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 10));
             }
             if ($distance_in_seconds < 20) {
                 return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 20));
             }
             if ($distance_in_seconds < 40) {
                 return $this->translator->trans('half a minute ago');
             }
             if ($distance_in_seconds < 60) {
                 return $this->translator->trans('less than a minute ago');
             }
             return $this->translator->trans('1 minute ago');
         }
         return $distance_in_minutes == 0 ? $this->translator->trans('less than a minute ago') : $this->translator->trans('1 minute ago');
     }
     if ($distance_in_minutes <= 45) {
         return $this->translator->trans('%minutes minutes ago', array('%minutes' => $distance_in_minutes));
     }
     if ($distance_in_minutes <= 90) {
         return $this->translator->trans('about 1 hour ago');
     }
     if ($distance_in_minutes <= 1440) {
         return $this->translator->trans('about %hours hours ago', array('%hours' => round($distance_in_minutes / 60)));
     }
     if ($distance_in_minutes <= 2880) {
         return $this->translator->trans('1 day ago');
     }
     $distance_in_days = round($distance_in_minutes / 1440);
     if (!$include_months || $distance_in_days <= 30) {
         return $this->translator->trans('%days days ago', array('%days' => round($distance_in_days)));
     }
     if ($distance_in_days < 345) {
         return $this->translator->transchoice('{1} 1 month ago |]1,Inf[ %months months ago', round($distance_in_days / 30), array('%months' => round($distance_in_days / 30)));
     }
     return $this->translator->transchoice('{1} 1 year ago |]1,Inf[ %years years ago', round($distance_in_days / 365), array('%years' => round($distance_in_days / 365)));
 }
 public function testReverseTransformExpectsValidDateString()
 {
     $reverseTransformer = new DateTimeToStringTransformer();
     $this->setExpectedException('Symfony\\Component\\Form\\Exception\\TransformationFailedException');
     $reverseTransformer->reverseTransform('2010-2010-2010', null);
 }
 /**
  * Reports the approximate distance in time between two times given in seconds
  * or in a valid ISO string like.
  * For example, if the distance is 47 minutes, it'll return
  * "about 1 hour". See the source for the complete wording list.
  *
  * Integers are interpreted as seconds. So, by example to check the distance of time between
  * a created user an it's last login:
  * {{ user.createdAt|distance_of_time_in_words(user.lastLoginAt) }} returns "less than a minute".
  *
  * Set include_seconds to true if you want more detailed approximations if distance < 1 minute
  *
  * @param $from_time String
  *            or DateTime
  * @param $to_time String
  *            or DateTime
  * @param bool $include_seconds
  *
  * @return mixed
  */
 public function distanceOfTimeInWordsFilter($from_time, $to_time = null, $include_seconds = false)
 {
     $datetime_transformer = new DateTimeToStringTransformer(null, null, 'Y-m-d H:i:s');
     $timestamp_transformer = new DateTimeToTimestampTransformer();
     // Transforming to DateTime
     $from_time = !$from_time instanceof \DateTime ? $datetime_transformer->reverseTransform($from_time) : $from_time;
     $to_time = empty($to_time) ? new \DateTime('now') : $to_time;
     $to_time = !$to_time instanceof \DateTime ? $datetime_transformer->reverseTransform($to_time) : $to_time;
     // Transforming to Timestamp
     $from_time = $timestamp_transformer->transform($from_time);
     $to_time = $timestamp_transformer->transform($to_time);
     $distance_in_minutes = round(abs($to_time - $from_time) / 60);
     $distance_in_seconds = round(abs($to_time - $from_time));
     if ($distance_in_minutes <= 1) {
         if ($include_seconds) {
             if ($distance_in_seconds < 5) {
                 return vsprintf('hace menos de %d segundos', array('%d' => 5));
             } elseif ($distance_in_seconds < 10) {
                 return vsprintf('hace menos de %d segundos', array('%d' => 10));
             } elseif ($distance_in_seconds < 20) {
                 return vsprintf('hace menos de %d segundos', array('%d' => 20));
             } elseif ($distance_in_seconds < 60) {
                 return vsprintf('hace menos de un minuto');
             } else {
                 return vsprintf('hace un minuto');
             }
         }
         return $distance_in_minutes === 0 ? 'hace menos de un minuto' : 'hace un minuto';
     } elseif ($distance_in_minutes <= 45) {
         return vsprintf('hace %d minutos', array('%d' => $distance_in_minutes));
     } elseif ($distance_in_minutes <= 90) {
         return 'hace una hora';
     } elseif ($distance_in_minutes <= 1440) {
         return vsprintf('hace unas %d horas', array('%d' => round($distance_in_minutes / 60)));
     } elseif ($distance_in_minutes <= 2880) {
         return 'ayer';
     } else {
         $distance_in_days = round($distance_in_minutes / 1440);
         if ($distance_in_days <= 60) {
             return vsprintf('hace %d días', array('%d' => $distance_in_days));
         } else {
             $distance_in_month = round($distance_in_days / 30);
             if ($distance_in_month < 20) {
                 return vsprintf('hace %d meses', array('%d' => $distance_in_month));
             } else {
                 $distance_in_year = abs(round($distance_in_month / 12, 1, PHP_ROUND_HALF_UP));
                 if (is_float($distance_in_year)) {
                     $year = (int) $distance_in_year;
                     $month = ($distance_in_year - $year) * 10;
                     if ($year == 1 and $month == 1) {
                         return vsprintf('hace %d año y %u mes', array('%d' => $year, '%u' => $month));
                     } else {
                         if ($year == 1 && $month > 1) {
                             return vsprintf('hace %d año y %u meses', array('%d' => $year, '%u' => $month));
                         } else {
                             if ($month == 1 && $year > 1) {
                                 return vsprintf('hace %d años y %u mes', array('%d' => $year, '%u' => $month));
                             } else {
                                 if ($month > 0) {
                                     return vsprintf('hace %d años y %u meses', array('%d' => $year, '%u' => $month));
                                 } else {
                                     return vsprintf('hace %d años', array('%d' => $year, '%u' => $month));
                                 }
                             }
                         }
                     }
                 } else {
                     if ($distance_in_year == 1) {
                         return vsprintf('hace %d año', array('%d' => $distance_in_year));
                     } else {
                         return vsprintf('hace %d años', array('%d' => $distance_in_year));
                     }
                 }
             }
         }
     }
 }