rearrange event formatting into function

This commit is contained in:
Christopher Teutsch 2020-02-21 22:22:22 +01:00
parent fad85e88e1
commit ad76a90850
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D

View File

@ -1,7 +1,7 @@
<?php
require_once 'vendor/autoload.php';
use ICal\ICal;
$DATE_FORMAT = 'd.m.Y H:i';
static $DATE_FORMAT = 'd.m.Y H:i';
$state_map = array(
'closed' => (object) array(
'state_string' => 'Das Dorf ist gerade <em>geschlossen</em>.',
@ -64,6 +64,18 @@ if ($locale !== null){
setlocale(LC_TIME, $locale);
}
$v = file_get_contents('/media/sg/dorf.jetzt_visitors');
function format_event($event): string{
global $DATE_FORMAT, $ical;
$startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]);
$startdate_str = $startdate_loop->format($DATE_FORMAT);
$interval_loop = new DateInterval($event->duration);
if ($interval_loop->d != 0 || $interval_loop->h >= 24){
$enddate_str = $startdate_loop->add($interval_loop)->format($DATE_FORMAT);
}else{
$enddate_str = $startdate_loop->add($interval_loop)->format('H:i');
}
return $startdate_str . ' &ndash; ' . $enddate_str;
}
?>
<!DOCTYPE html>
<html lang="de">
@ -96,23 +108,14 @@ $v = file_get_contents('/media/sg/dorf.jetzt_visitors');
<h6 class="card-subtitle mb-2 text-muted">Nächste Veranstaltung</h6>
<p class="card-text">
<h5 class="font-weight-bold"><?= htmlspecialchars($first_event->summary, ENT_QUOTES, 'UTF-8') ?></h5>
<span class="font-weight-normal"><?= $ical->iCalDateToDateTime($first_event->dtstart_array[3])->format($DATE_FORMAT) ?></span>
<span class="font-weight-normal"><?= format_event($first_event) ?></span>
</p>
<h5 class="card-title">Danach:</h5>
<table class="table">
<tbody>
<?php foreach ($events as $event) : ?>
<tr>
<th scope="row"><?php
$startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]);
$startdate_str = $startdate_loop->format($DATE_FORMAT);
$interval_loop = new DateInterval($event->duration);
if ($interval_loop->d != 0 || $interval_loop->h >= 24){
$enddate_str = $startdate_loop->add($interval_loop)->format($DATE_FORMAT);
}else{
$enddate_str = $startdate_loop->add($interval_loop)->format('H:i');
}
echo($startdate_str . ' &ndash; ' . $enddate_str); ?></th>
<th scope="row"><?=format_event($event)?></th>
<td><?= htmlspecialchars($event->summary) ?></td>
</tr>
<?php endforeach ?>