/** * @param \Datetime|string|int $time * @param string|null $pattern * @param string|null $locale * @param string|null $timezone * @param string|null $timeType * * @return string */ public function formatTime($time, $pattern = null, $locale = null, $timezone = null, $timeType = null) { if ($pattern) { return $this->helper->format($time, $pattern, $locale, $timezone); } return $this->helper->formatTime($time, $locale, $timezone, $timeType); }
protected function prepareMessage(Distance $distance, $include_seconds, $include_months) { $distance_in_minutes = $distance->getMinutes(); $distance_in_seconds = $distance->getSeconds(); if ($distance_in_seconds <= 60) { if ($include_seconds) { if ($distance_in_seconds < 20) { return $this->translator->transchoice('less than %seconds seconds ago', $distance_in_seconds, array('%seconds' => $distance_in_seconds)); } 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->transchoice('%minutes minutes ago', $distance_in_minutes, array('%minutes' => $distance_in_minutes)); } elseif ($distance_in_minutes <= 90) { return $this->translator->trans('about 1 hour ago'); } elseif ($distance_in_minutes <= 240) { return $this->translator->transchoice('about %hours hours ago', $distance->getHours(), array('%hours' => $distance->getHours())); } elseif ($distance->isToday()) { return $this->translator->trans('today at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT))); } elseif ($distance->isYesterday()) { return $this->translator->trans('yersterday at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT))); } elseif ($distance->isThisYear()) { return $this->translator->trans('%date at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT), '%date' => $this->datetimeformater->format($distance->getFrom(), "d LLL"))); } else { return $this->translator->trans('%date', array('%date' => $this->datetimeformater->format($distance->getFrom(), "d LLL yyyy"))); } }
public function testLocale() { $localeDetector = $this->getMock('Sonata\\IntlBundle\\Locale\\LocaleDetectorInterface'); $localeDetector->expects($this->any())->method('getLocale')->will($this->returnValue('fr')); $timezoneDetector = $this->getMock('Sonata\\IntlBundle\\Timezone\\TimezoneDetectorInterface'); $timezoneDetector->expects($this->any())->method('getTimezone')->will($this->returnValue('Europe/Paris')); $helper = new DateTimeHelper($timezoneDetector, 'UTF-8', $localeDetector); $datetimeLondon = new \DateTime(); $datetimeLondon->setDate(1981, 11, 30); $datetimeLondon->setTime(2, 0, 0); $datetimeLondon->setTimezone(new \DateTimeZone('Europe/London')); $datetimeParis = $helper->getDatetime('1981-11-30 02:00'); $this->assertEquals('Mon, 30 Nov 1981 01:00:00 +0000', $datetimeLondon->format('r')); $this->assertEquals('Mon, 30 Nov 1981 02:00:00 +0100', $datetimeParis->format('r')); // check convertor $this->assertEquals(375930000, $helper->getDatetime($datetimeParis)->format('U')); $this->assertEquals(375930000, $helper->getDatetime($datetimeLondon)->format('U')); // warning .. this use value php.ini's timezone configuration $this->assertEquals(1293708203, $helper->getDatetime('2010-12-30 12:23:23')->format('U')); $this->assertEquals(1293663600, $helper->getDatetime('2010-12-30')->format('U')); $this->assertEquals(1293708180, $helper->getDatetime('2010-12-30 12:23')->format('U')); // check default method $this->assertEquals('30 nov. 1981', $helper->formatDate($datetimeParis)); $this->assertEquals('30 déc. 2010', $helper->formatDate('2010-12-30 12:23:23')); $this->assertEquals('02:00:00', $helper->formatTime($datetimeParis)); $this->assertEquals('30 nov. 1981 02:00:00', $helper->formatDateTime($datetimeParis)); // custom format $this->assertEquals('30 novembre 1981', $helper->formatDate($datetimeParis, null, null, \IntlDateFormatter::LONG)); if (version_compare(DateTimeHelper::getICUDataVersion(), '52', '>=')) { $this->assertEquals('02:00:00 UTC+1', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } elseif (version_compare(DateTimeHelper::getICUDataVersion(), '4.8.0', '>=')) { $this->assertEquals('02:00:00 UTC+01:00', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } else { $this->assertEquals('02:00:00 HNEC', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } $this->assertEquals('30 novembre 1981 02:00', $helper->formatDateTime($datetimeParis, null, null, \IntlDateFormatter::LONG, \IntlDateFormatter::SHORT)); $this->assertEquals('30 nov. 1981 ap. J.-C.', $helper->format($datetimeParis, 'dd MMM Y G')); }