/** * @param int $nDays * * @return static */ public static function fromInteger($nDays) { $i = MiscHelpers::binarySearch($nDays, static::$fiveLeapYearsToInt); /** @var static $upper upper-bound*/ $upper = new static(static::$fiveLeapYears[$i], 12, 30); /** @var int $delta2 the difference between upper-bound ($upper) and the input ($nDays) in days*/ $delta2 = $upper->toInteger() - $nDays; if (!$delta2) { return $upper; } if ($delta2 <= 365) { $year = $upper->getYear(); /** @var static $lower lower-bound */ $lower = new static($year - 1, 12, 29); /** @var int $delta1 the difference between the input ($nDays) and lower-bound ($lower) in days */ $delta1 = $nDays - $lower->toInteger(); $month = MiscHelpers::binarySearch($delta1, static::$cumulativeDaysInMonth); $day = $delta1 - static::$cumulativeDaysInMonth[$month - 1]; return new static($year, $month, $day); } if ($delta2 <= 5 * 365) { $lower = new static($upper->getYear() - 5, 12, 30); $delta1 = $nDays - $lower->toInteger(); $year = $lower->getYear() + ceil($delta1 / 365); $lower = new static($year, 1, 1); $delta1 = $nDays - $lower->toInteger() + 1; $month = MiscHelpers::binarySearch($delta1, static::$cumulativeDaysInMonth); $day = $delta1 - static::$cumulativeDaysInMonth[$month - 1]; return new static($year, $month, $day); } /** @var static $lower last 5-leap year*/ $lower = new static(static::$fiveLeapYears[$i - 1] + 1, 1, 1); $delta1 = $nDays - $lower->toInteger() + 1; /** @var static $lower last leap year*/ $lower = new static($lower->getYear() + 4 * (int) ($delta1 / (4 * 365 + 1)), 1, 1); $delta1 = $nDays - $lower->toInteger() + 1; $year = $lower->getYear() - 1 + ceil($delta1 / 365); $lower = new static($year, 1, 1); $delta1 = $nDays - $lower->toInteger() + 1; $month = MiscHelpers::binarySearch($delta1, static::$cumulativeDaysInMonth); $day = $delta1 - static::$cumulativeDaysInMonth[$month - 1]; return new static($year, $month, $day); }
/** * Make and return a new ExpressiveDate instance with defined year, month, day, hour, minute, and second. * * @param int $year * @param int $month * @param int $day * @param int $hour * @param int $minute * @param int $second * @param string|DateTimeZone $timezone * @return ExpressiveDate */ public static function makeFromDateTime($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) { $date = new static(null, $timezone); $date->setDate($year ?: $date->getYear(), $month ?: $date->getMonth(), $day ?: $date->getDay()); // If no hour was given then we'll default the minute and second to the current // minute and second. If a date was given and minute or second are null then // we'll set them to 0, mimicking PHPs behaviour. if (is_null($hour)) { $minute = $minute ?: $date->getMinute(); $second = $second ?: $date->getSecond(); } else { $minute = $minute ?: 0; $second = $second ?: 0; } $date->setTime($hour ?: $date->getHour(), $minute, $second); return $date; }