Date Manipulation
@param type $date
@param type $format
@return type date
323899
public function getDate($date = null, $format = 'Y-m-d H:i:s')
    {
        if (strlen($date) < 0) {
            $date = date('d-m-Y H:i:s');
        }

        $timeStamp = new DateTime($date);
        return $timeStamp->format($format);
    }
947818
public static function date2str($date, $format = '%d/%m/%Y')
    {
        $dt = new self();
        if (!$dt->set_date($date)) {
            return '';
        }
        
        return $dt->format($format);
    }
479630
public static Calendar parse(final String date, final String format) {
		return parse(date, DateTimeFormat.forPattern(format));
	}
861342
public function translate($date): Date
    {
        $handle = Date::castFrom($date)->handle();
        $handle->setTimezone($this->timeZone);
        return new Date($handle);
    }
734661
public static function formatDate($date)
    {
        $format = DateTime::ATOM;
        if ($date instanceof DateTime) {
            $d = $date->format($format);
        } elseif (is_numeric($date)) {
            $d = date($format, $date);
        } else {
            $d = (string) $date;
        }
        return $d;
    }
984912
public static function DateTime ($value, $format) {
		try {
			return (new \DateTime($value))->format($format) === $value;
		} catch (\Exception $e) {
			return false;
		}
	}
356630
public function format($format)
    {
        if(Format::exist($format))
            $this->format = Format::get($format);
        else
            Format::invalidType();

        return $this;
    }
1357724
public function setPublishedFrom( $date, $format = null )
    {
        $this->publishedFrom = empty( $date ) ? null : $this->inputDate( $date, $format );
        return $this;
    }
670001
private static function parseDateFormat($format, $date)
    {
        $arDate = date_parse_from_format($format, $date);
        
        return [
            'arYear'  => ($arDate['year'] != false) ? $arDate['year'] : null,
            'arMonth' => ($arDate['month'] != false) ? $arDate['month'] : null,
            'arDay'   => ($arDate['day'] != false) ? $arDate['day'] : null,
            'hour'    => ($arDate['hour'] != false) ? $arDate['hour'] : null,
            'minute'  => ($arDate['minute'] != false) ? $arDate['minute'] : null,
            'second'  => ($arDate['second'] != false) ? $arDate['second'] : null,
        ];
    }
681352
public static function DateFormat($format, $date = null)
  {
    if (!empty($date) && !is_int($date)) {
      $date = strtotime($date);
    }

    return strftime($format, $date ?: time());
  }
1322307
public function setFormat($format)
    {
        if (!is_null($format)) {
            $this->format = $format;
            $this->useFormat(false);
        }

        return $this;
    }
1374947
public static function parseFromFormat($format, $date)
    {
        // reverse engineer date formats
        $keys = [
            'Y' => ['year', '\d{4}'],
            'y' => ['year', '\d{2}'],
            'm' => ['month', '\d{2}'],
            'n' => ['month', '\d{1,2}'],
            'M' => ['month', '[A-Z][a-z]{3}'],
            'F' => ['month', '[A-Z][a-z]{2,8}'],
            'd' => ['day', '\d{2}'],
            'j' => ['day', '\d{1,2}'],
            'D' => ['day', '[A-Z][a-z]{2}'],
            'l' => ['day', '[A-Z][a-z]{6,9}'],
            'u' => ['hour', '\d{1,6}'],
            'h' => ['hour', '\d{2}'],
            'H' => ['hour', '\d{2}'],
            'g' => ['hour', '\d{1,2}'],
            'G' => ['hour', '\d{1,2}'],
            'i' => ['minute', '\d{2}'],
            's' => ['second', '\d{2}'],
        ];
        // convert format string to regex
        $regex = '';
        $chars = str_split($format);
        foreach ($chars as $n => $char)
        {
            $lastChar = isset($chars[ $n - 1 ]) ? $chars[ $n - 1 ] : '';
            $skipCurrent = '\\' == $lastChar;
            if (!$skipCurrent && isset($keys[ $char ]))
            {
                $regex .= '(?P<' . $keys[ $char ][0] . '>' . $keys[ $char ][1] . ')';
            }
            else
            {
                if ('\\' == $char)
                {
                    $regex .= $char;
                }
                else
                {
                    $regex .= preg_quote($char);
                }
            }
        }
        $dt = [];
        $dt['error_count'] = 0;
        // now try to match it
        if (preg_match('#^' . $regex . '$#', $date, $dt))
        {
            foreach ($dt as $k => $v)
            {
                if (is_int($k))
                {
                    unset($dt[ $k ]);
                }
            }
            if (!jDateTime::checkdate($dt['month'], $dt['day'], $dt['year'], false))
            {
                $dt['error_count'] = 1;
            }
        }
        else
        {
            $dt['error_count'] = 1;
        }
        $dt['errors'] = [];
        $dt['fraction'] = '';
        $dt['warning_count'] = 0;
        $dt['warnings'] = [];
        $dt['is_localtime'] = 0;
        $dt['zone_type'] = 0;
        $dt['zone'] = 0;
        $dt['is_dst'] = '';
        if (strlen($dt['year']) == 2)
        {
            $now = jDate::forge('now');
            $x = $now->format('Y') - $now->format('y');
            $dt['year'] += $x;
        }
        $dt['year'] = isset($dt['year']) ? (int)$dt['year'] : 0;
        $dt['month'] = isset($dt['month']) ? (int)$dt['month'] : 0;
        $dt['day'] = isset($dt['day']) ? (int)$dt['day'] : 0;
        $dt['hour'] = isset($dt['hour']) ? (int)$dt['hour'] : 0;
        $dt['minute'] = isset($dt['minute']) ? (int)$dt['minute'] : 0;
        $dt['second'] = isset($dt['second']) ? (int)$dt['second'] : 0;
        $dt['just_time'] = $dt['hour'] * 360 + $dt['minute'] * 60 + $dt['second'];

        return $dt;
    }
444531
public function format($date, $format) {

        // Si ce n'est un objet Date, on le crée.
        if (!($date instanceof Date)) {
            $date = new Date($date);
        }

        // Si un fuseau horaire utilisateur est spécifié dans l'appli.
        if (!empty($this->tz)) {
            $date->setTimezone(new \DateTimeZone($this->tz));
        }

        // Si le format est une chaine traduisible (format différent suivant la langue de l'utilisateur)
        if ($this->lang->hasKey($format)) {
            $format = $this->lang->translate($format);
        }

        setlocale(LC_TIME, $this->lang->getLocale());
        return strftime($format, $date->getTimestamp());

    }
300647
public static function convertToDatetime($rawDate, $format = 'd.m.Y')
    {
        // convert timestamp to date format
        if (Any::isInt($rawDate)) {
            $rawDate = date(\DateTime::ATOM, $rawDate);
        }

        try {
            $object = new \DateTime($rawDate);
            return $object->format($format);
        } catch (\Exception $e) {
            return false;
        }
    }
467422
public function rule_date_format( $key, $value, $format = 'd/m/Y' ) 
	{
		return date( $format, strtotime( trim( $value ) ) ) == trim( $value );
	}
1204142
private function isValidDate(string $date, string $format = 'Y-m-d') : bool
    {
        $d = DateTime::createFromFormat($format, $date);
        // The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
        return $d && $d->format($format) === $date;
    }
710640
static function fromDate($date = false)
    {
        $format = count(explode(" ", $date)) > 1 ? Settings\DefaultDateSettings::$defaultDateTimeFromat : Settings\DefaultDateSettings::$defaultDateFromat;
        return ($date) ? static::createFromFormat($format, $date) : new self();
    }