Exemplo n.º 1
0
 /**
  * Localization
  * @param string $type Type of localization
  * @param \DateTime|int|string $data parameters for this localization
  * @param array $options
  * @return string|int|false
  *
  * Returns the localized data.
  *
  * Implemented types:
  *  - date
  *    - Creates a date
  *    - params: timestamp (int/string)
  *  - datetime
  *    - Creates date and time
  *    - params: timestamp (int/string)
  *  - time
  *    - Creates a time
  *    - params: timestamp (int/string)
  *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
  *  - jsdate: Returns the short JS date format
  */
 public function l($type, $data = null, $options = array())
 {
     // Use the language of the instance
     $locale = $this->getLanguageCode();
     if ($locale === 'sr@latin') {
         $locale = 'sr_latn';
     }
     if ($type === 'firstday') {
         return (int) Calendar::getFirstWeekday($locale);
     }
     if ($type === 'jsdate') {
         return (string) Calendar::getDateFormat('short', $locale);
     }
     $value = new \DateTime();
     if ($data instanceof \DateTime) {
         $value = $data;
     } else {
         if (is_string($data) && !is_numeric($data)) {
             $data = strtotime($data);
             $value->setTimestamp($data);
         } else {
             if ($data !== null) {
                 $value->setTimestamp($data);
             }
         }
     }
     $options = array_merge(array('width' => 'long'), $options);
     $width = $options['width'];
     switch ($type) {
         case 'date':
             return (string) Calendar::formatDate($value, $width, $locale);
         case 'datetime':
             return (string) Calendar::formatDatetime($value, $width, $locale);
         case 'time':
             return (string) Calendar::formatTime($value, $width, $locale);
         default:
             return false;
     }
 }
Exemplo n.º 2
0
 /**
  * @param string|array $type
  *
  * @return string
  */
 public function display($type = null) : string
 {
     if ($type == self::DISPLAY_DURATION) {
         return $this->diffForHumans(DateTime::now(), true);
     }
     if (is_null($type)) {
         $type = [self::DISPLAY_SHORT, self::DISPLAY_SHORT];
     } elseif (!is_array($type)) {
         $type = [$type, $type];
     } elseif (is_array($type)) {
         if (!isset($type[1])) {
             return Calendar::formatDate($this, $type[0]);
         } elseif (is_null($type[0])) {
             return Calendar::formatTime($this, $type[1]);
         }
     }
     return Calendar::formatDatetime($this, implode('|', $type));
 }
Exemplo n.º 3
0
 /**
  * @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);
 }
Exemplo n.º 4
0
 /**
  * Render the time part of a date/time as a localized string.
  *
  * @param mixed $value The date/time representation (one of the values accepted by toDateTime)
  * @param bool $withSeconds Set to true to include seconds (eg '11:59:59 PM'), false (default) otherwise (eg '11:59 PM');
  * @param string $toTimezone The timezone to set. Special values are:<ul>
  *     <li>'system' for the current system timezone</li>
  *     <li>'user' (default) for the user's timezone</li>
  *     <li>'app' for the app's timezone</li>
  *     <li>Other values: one of the PHP supported time zones (see http://us1.php.net/manual/en/timezones.php )</li>
  * </ul>
  *
  * @return string Returns an empty string if $value couldn't be parsed, the localized string otherwise
  */
 public function formatTime($value = 'now', $withSeconds = false, $toTimezone = 'user')
 {
     return Calendar::formatTime($this->toDateTime($value, $toTimezone), $withSeconds ? 'medium' : 'short');
 }