/** * @param string $type * * @return string */ public function display($type = self::DISPLAY_SHORT) : string { if ($type == self::DISPLAY_DURATION) { $format = []; if ($this->hour) { $format[] = Unit::format($this->hour, 'duration/hour'); } if ($this->minute) { $format[] = Unit::format($this->minute, 'duration/minute'); } if ($this->second) { $format[] = Unit::format($this->second, 'duration/second'); } return Misc::joinUnits($format, 'narrow'); } return Calendar::formatTime($this, $type); }
/** * {@inheritdoc} */ public function __construct(string $name, string $label = null) { parent::__construct($name, $label); self::translator()->addFile(__DIR__ . '/../../../lang/global', 'bootstrap'); $this->add((new Group(self::trans('bootstrap.googlePlace/street')))->add($this->fields['number'] = (new Text($name . '[number]'))->setPlaceholder(self::trans('bootstrap.googlePlace/number')))->add($this->fields['street'] = (new Text($name . '[street]'))->setPlaceholder(self::trans('bootstrap.googlePlace/street'))))->add((new Group(Misc::joinUnits([self::trans('bootstrap.googlePlace/zipcode'), self::trans('bootstrap.googlePlace/city')])))->add($this->fields['zipcode'] = (new Text($name . '[zipcode]'))->setPlaceholder(self::trans('bootstrap.googlePlace/zipcode')))->add($this->fields['city'] = (new Text($name . '[city]'))->setPlaceholder(self::trans('bootstrap.googlePlace/city'))))->add($this->fields['state'] = new Text($name . '[state]', self::trans('bootstrap.googlePlace/state')))->add($this->fields['country'] = new Select($name . '[country]', self::trans('bootstrap.googlePlace/country'), Territory::getCountries()))->add($this->fields['lat'] = new Hidden($name . '[lat]'))->add($this->fields['long'] = new Hidden($name . '[long]')); }
/** * Describe an interval between two dates (eg '2 days and 4 hours'). * @param \DateTime $dateEnd The first date * @param \DateTime|null $dateStart The final date (if it has a timezone different than $dateEnd, we'll use the one of $dateEnd) * @param int $maxParts = 2 The maximim parts (eg with 2 you may have '2 days and 4 hours', with 3 '2 days, 4 hours and 24 minutes') * @param string $width = 'short' The format name; it can be 'long' (eg '3 seconds'), 'short' (eg '3 s') or 'narrow' (eg '3s') * @param string $locale = '' The locale to use. If empty we'll use the default locale set in \Punic\Data * @return string * @throws Exception\BadArgumentType */ public static function describeInterval($dateEnd, $dateStart = null, $maxParts = 2, $width = 'short', $locale = '') { if (!is_a($dateEnd, '\\DateTime')) { throw new Exception\BadArgumentType($dateEnd, '\\DateTime'); } if (empty($dateStart) && $dateStart !== 0 && $dateStart !== '0') { $dateStart = new \DateTime('now', $dateEnd->getTimezone()); } if (!is_a($dateStart, '\\DateTime')) { throw new Exception\BadArgumentType($dateStart, '\\DateTime'); } if ($dateStart->getOffset() !== $dateEnd->getOffset()) { $dateStart->setTimezone($dateEnd->getTimezone()); } $utc = new \DateTimeZone('UTC'); $dateEndUTC = new \DateTime($dateEnd->format('Y-m-d H:i:s'), $utc); $dateStartUTC = new \DateTime($dateStart->format('Y-m-d H:i:s'), $utc); $parts = array(); $data = \Punic\Data::get('dateFields', $locale); if ($dateEndUTC->getTimestamp() == $dateStartUTC->getTimestamp()) { $parts[] = $data['second']['relative-type-0']; } else { $diff = $dateStartUTC->diff($dateEndUTC, true); $mostFar = 0; $maxDistance = 3; if ($mostFar < $maxDistance && $diff->y > 0) { $parts[] = \Punic\Unit::format($diff->y, 'duration/year', $width, $locale); $mostFar = 0; } elseif (!empty($parts)) { $mostFar++; } if ($mostFar < $maxDistance && $diff->m > 0) { $parts[] = \Punic\Unit::format($diff->m, 'duration/month', $width, $locale); $mostFar = 0; } elseif (!empty($parts)) { $mostFar++; } if ($mostFar < $maxDistance && $diff->d > 0) { $parts[] = \Punic\Unit::format($diff->d, 'duration/day', $width, $locale); $mostFar = 0; } elseif (!empty($parts)) { $mostFar++; } if ($mostFar < $maxDistance && $diff->h > 0) { $parts[] = \Punic\Unit::format($diff->h, 'duration/hour', $width, $locale); $mostFar = 0; } elseif (!empty($parts)) { $mostFar++; } if ($mostFar < $maxDistance && $diff->i > 0) { $parts[] = \Punic\Unit::format($diff->i, 'duration/minute', $width, $locale); $mostFar = 0; } elseif (!empty($parts)) { $mostFar++; } if (empty($parts) || $diff->s > 0) { $parts[] = \Punic\Unit::format($diff->s, 'duration/second', $width, $locale); } if (count($parts) > $maxParts) { $parts = array_slice($parts, 0, $maxParts); } } switch ($width) { case 'narrow': case 'short': $joined = \Punic\Misc::joinUnits($parts, $width, $locale); break; default: $joined = \Punic\Misc::join($parts, $locale); break; } return $joined; }