Jyxo\Time\Time::format PHP Method

format() public method

Returns date/time in the given format with months and days translated.
public format ( string $format, string | DateTimeZone $timeZone = null ) : string
$format string Requested format
$timeZone string | DateTimeZone Result time zone definition
return string
    public function format(string $format, $timeZone = null) : string
    {
        // Prepares the right result time zone if needed
        if ($timeZone) {
            $this->setTemporaryTimeZone($timeZone);
        }
        // Translation required?
        if (preg_match('~(?:^|[^\\\\])[lDFM]~', $format)) {
            static $days = [];
            if (empty($days)) {
                $days = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday')];
            }
            static $daysShort = [];
            if (empty($daysShort)) {
                $daysShort = [_('Mon'), _('Tue'), _('Wed'), _('Thu'), _('Fri'), _('Sat'), _('Sun')];
            }
            static $months = [];
            if (empty($months)) {
                $months = [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'), _('September'), _('October'), _('November'), _('December')];
            }
            static $monthsGen = [];
            if (empty($monthsGen)) {
                $monthsGen = [_('January#~Genitive'), _('February#~Genitive'), _('March#~Genitive'), _('April#~Genitive'), _('May#~Genitive'), _('June#~Genitive'), _('July#~Genitive'), _('August#~Genitive'), _('September#~Genitive'), _('October#~Genitive'), _('November#~Genitive'), _('December#~Genitive')];
            }
            static $monthsShort = [];
            if (empty($monthsShort)) {
                $monthsShort = [_('Jan'), _('Feb'), _('Mar'), _('Apr'), _('May#~Shortcut'), _('Jun'), _('Jul'), _('Aug'), _('Sep'), _('Oct'), _('Nov'), _('Dec')];
            }
            // Replace certain identifiers with fake ones
            $search = ['~(^|[^\\\\])l~', '~(^|[^\\\\])D~', '~(^|[^\\\\])F~', '~(^|[^\\\\])M~'];
            $replace = ['$1<===>', '$1<___>', '$1<--->', '$1<...>'];
            $format = preg_replace($search, $replace, $format);
            // Format the rest of the date
            $date = $this->dateTime->format($format);
            // Calculate day and month
            $day = $this->dateTime->format('N') - 1;
            $month = $this->dateTime->format('n') - 1;
            // If the month is not at the beginning, the genitive case and lowercase will be used
            $monthName = 0 !== strpos($format, '<--->') ? mb_strtolower($monthsGen[$month], 'utf-8') : $months[$month];
            // Add translated days and months into the result
            $result = strtr($date, ['<===>' => $days[$day], '<___>' => $daysShort[$day], '<--->' => $monthName, '<...>' => $monthsShort[$month]]);
        } else {
            // No need to translate
            $result = $this->dateTime->format($format);
        }
        // If a custom result timezone was specified, revert the original one
        if ($timeZone) {
            $this->revertOriginalTimeZone();
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * Checks if the given date is a working day.
  *
  * @param \Jyxo\Time\Time $day Date to be checked
  * @return boolean
  */
 public static function isWorkDay(\Jyxo\Time\Time $day)
 {
     $holidays = self::$holidays;
     // Adds Easter Monday. easter_date is supposed to be buggy http://cz.php.net/manual/en/function.easter-date.php#80664
     $year = (int) $day->format('Y');
     $days = easter_days($year);
     // $days returns the number of days from March 21st until the Easter Sunday, +1 because of Monday
     $holidays[] = date('j.n', strtotime($year . '-03-21 +' . ($days + 1) . ' days'));
     $isWorkDay = true;
     if ($day->format('N') > 5) {
         // Saturday or Sunday
         $isWorkDay = false;
     } elseif (in_array($day->format('j.n'), $holidays)) {
         // Public holiday, hurray!
         $isWorkDay = false;
     }
     return $isWorkDay;
 }