forecast = $t; } /** * «Tonight Southeasterly light breeze, cloudy, temperature -8 to -9 degrees» * @author Helge Tangen */ public function start(): string { $time = $this->forecast->getVariations()->getTime(); return sprintf( "%s from %s (%s) %s", $this->when($time->getFrom()), $time->getFrom()->format('H'), $time->getFrom()->format('h a'), (function() use ($time){ $str = []; $prev = null; foreach ($time as $data) { switch (get_class($data)) { case Symbol::class: $str[] = ($prev == null ? ' ' : ', ') . $data->getName(); break; case WindSpeed::class: $str[] = ($prev == null || WindSpeed::class ? " " : ", ") . $data->getName(); break; case WindDirection::class: $str[] = ($prev == null ? ' ' : ', ') . $data->getName(); break; case Temperature::class: $data = $data->convertTo(Temperature::UNIT_KELVIN); $str[] = ($prev == null ? ' ' : ', ') . sprintf('%d %s', $data->getValue(), $data->getDegree()); break; default: //$str []= '``' . get_class($data) . '`` missing'; } $prev = $data; } $last = array_pop($str); if (substr($last, 0, 1) == ',') $last = substr($last, 1); if (sizeof($str) == 0) return strtolower($last); return strtolower(sprintf( "%s and %s", join($str), $last )); })() ); } public function when(\DateTimeInterface $d): string { $diff = $d->diff(new \DateTime()); if ($diff->invert && $diff->days == 1) return sprintf("yesterday %s", $this->period($d)); elseif (!$diff->invert && $diff->days == 1) return sprintf("tomorrow %s", $this->period($d)); elseif ($diff->days == 0) return sprintf("This %s", $this->period($d)); return sprintf("the %s at %s of %s %y ", $this->period($d), $d->format('dd'), $d->format('m'), $d->format('YY') ); } public function period(\DateTimeInterface $d): string { $hour = (int)$d->format('H'); if ($hour >= 6 && $hour <= 11) return "morning"; elseif ($hour == 12) return "noon"; elseif ($hour > 12 && $hour < 18) return "afternoon"; elseif ($hour >= 18 && $hour < 24) return "evening"; elseif ($hour == 12) return "midning"; else return "night"; } public function since(\DateTimeInterface $d): string { } } $url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml'; $forecast = new Forecast($url); $range = $forecast->getTabular()->getBetween( $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D')) ); echo (new TextForcast($range))->start(); ?>