Jyxo\Time\Time::truncate PHP Method

truncate() public method

Returns a new instance with date/time truncated to the given unit.
public truncate ( string $unit ) : self
$unit string Unit to truncate the date/time to
return self
    public function truncate($unit) : self
    {
        $dateTime = [self::YEAR => 0, self::MONTH => 1, self::DAY => 1, self::HOUR => 0, self::MINUTE => 0, self::SECOND => 0];
        switch ((string) $unit) {
            case self::SECOND:
                $dateTime[self::SECOND] = $this->dateTime->format('s');
                // Intentionally missing break
            // Intentionally missing break
            case self::MINUTE:
                $dateTime[self::MINUTE] = $this->dateTime->format('i');
                // Intentionally missing break
            // Intentionally missing break
            case self::HOUR:
                $dateTime[self::HOUR] = $this->dateTime->format('H');
                // Intentionally missing break
            // Intentionally missing break
            case self::DAY:
                $dateTime[self::DAY] = $this->dateTime->format('d');
                // Intentionally missing break
            // Intentionally missing break
            case self::MONTH:
                $dateTime[self::MONTH] = $this->dateTime->format('m');
                // Intentionally missing break
            // Intentionally missing break
            case self::YEAR:
                $dateTime[self::YEAR] = $this->dateTime->format('Y');
                break;
            default:
                throw new \InvalidArgumentException(sprintf('Time unit %s is not defined.', $unit));
        }
        return new self(vsprintf('%s-%s-%sT%s:%s:%s', $dateTime), $this->dateTime->getTimezone());
    }