Skip to content

Commit

Permalink
Merge pull request #35 from rmeister/fix-strftime
Browse files Browse the repository at this point in the history
Fix: deprecated strftime with IntlDateFormatter
  • Loading branch information
mzur authored May 14, 2024
2 parents 95d271b + 53019f1 commit 56340ae
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
73 changes: 37 additions & 36 deletions Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ function __construct($event) {
$this->hasBeginTime = (bool) A::get($event, self::BEGIN_TIME_KEY);
$this->hasEndTime = (bool) A::get($event, self::END_TIME_KEY);

$this->beginTimestamp = self::getTimestamp(
$this->beginTimestamp = self::getDate(
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::BEGIN_TIME_KEY)
);

$this->endTimestamp = self::getTimestamp(
$this->endTimestamp = self::getDate(
A::get($event, self::END_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);

// if there is no end date given, use the same as the beginning date
if (!$this->endTimestamp) {
$this->endTimestamp = self::getTimestamp(
$this->endTimestamp = self::getDate(
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);
Expand All @@ -109,7 +109,7 @@ function __construct($event) {

// if there is no end time given, the event lasts until end of the day
if (!$this->hasEndTime) {
$this->endTimestamp = strtotime('tomorrow', $this->endTimestamp);
$this->endTimestamp->setTime(23, 59, 59);
}

// only use the full format, if there were times given for this event
Expand All @@ -136,7 +136,7 @@ public static function instantiate($event) {
* same time and > 0 if $e2 is older than $e1
*/
public static function compare($e1, $e2) {
return $e1->beginTimestamp - $e2->beginTimestamp;
return $e1->beginTimestamp <=> $e2->beginTimestamp;
}

/**
Expand Down Expand Up @@ -166,11 +166,15 @@ private static function validate($event) {
/**
* @param string $date the date, e.g. '01.01.1970'
* @param string $time optional time, e.g. '10:00:00'
* @return The date as a UNIX timestamp or <code>false</code> if there
* @return The date as a DateTimeImmutable object or <code>false</code> if there
* was no $date given.
*/
private static function getTimestamp($date, $time = '') {
return ($date) ? strtotime($date . ' ' . $time) : false;
private static function getDate($date, $time = '') {
if ($date) {
return new \DateTimeImmutable($date . ' ' . $time);
} else {
return false;
}
}

/**
Expand All @@ -191,75 +195,72 @@ private static function filterFields($event) {
* @return The timestamp in seconds for the beginning of this event.
*/
public function getBeginTimestamp() {
return $this->beginTimestamp;
return $this->beginTimestamp->getTimestamp();
}

/**
* @return The date array of the beginning of this event.
*/
public function getBeginDate() {
return getdate($this->beginTimestamp);
return getdate($this->beginTimestamp->getTimestamp());
}

/**
* @param string $languageCode the language used to create the date string, e.g. 'de'
* @return The formatted string of the beginning of this event. Formatting
* is done according to the language configuration of Kirby.
* is done according to the language code given as argument.
*/
public function getBeginStr() {
return strftime($this->timeFormat, $this->beginTimestamp);
public function getBeginStr($languageCode) {
return \IntlDateFormatter::formatObject(
$this->beginTimestamp,
$this->timeFormat,
$languageCode);
}

/**
* @return The formatted string of the beginning of this event wrapped in
* a <code>time</code> element with <code>datetime</code> attribute.
*/
public function getBeginHtml() {
public function getBeginHtml($languageCode = 'en') {
return '<time datetime="' .
gmdate('Y-m-d\TH:i:s\Z', $this->beginTimestamp) . '">' .
$this->getBeginStr() . '</time>';
gmdate('Y-m-d\TH:i:s\Z', $this->beginTimestamp->getTimestamp()) . '">' .
$this->getBeginStr($languageCode) . '</time>';
}

/**
* @return The timestamp in seconds for the ending of this event.
*/
public function getEndTimestamp() {
return $this->endTimestamp;
return $this->endTimestamp->getTimestamp();
}

/**
* @return The date array of the ending of this event.
*/
public function getEndDate() {
return getdate($this->endTimestamp);
return getdate($this->endTimestamp->getTimestamp());
}

/**
* @param string $languageCode the language used to create the date string, e.g. 'de'
* @return The formatted string of the ending of this event. Formatting
* is done according to the language configuration of Kirby.
* is done according to the language code given as argument.
*/
public function getEndStr() {
/*
* The convention for an event lasting all day is from midnight of the
* day to midnight of the following day. But if we have an event lasting
* from the 14th to 15th it would be printed as 14th (12 am) to 16th
* (12 am).
* So if there is no custom ending time given, we go one second back, so
* it prints as 14th (12 am) to 15th (11:59:59 pm).
*/
$timestamp = ($this->hasEndTime)
? $this->endTimestamp
: $this->endTimestamp - 1;
return strftime($this->timeFormat, $timestamp);
public function getEndStr($languageCode) {
return \IntlDateFormatter::formatObject(
$this->endTimestamp,
$this->timeFormat,
$languageCode);
}

/**
* @return The formatted string of the ending of this event wrapped in
* a <code>time</code> element with <code>datetime</code> attribute.
*/
public function getEndHtml() {
public function getEndHtml($languageCode = 'en') {
return '<time datetime="' .
gmdate('Y-m-d\TH:i:s\Z', $this->endTimestamp) . '">' .
$this->getEndStr() . '</time>';
gmdate('Y-m-d\TH:i:s\Z', $this->endTimestamp->getTimestamp()) . '">' .
$this->getEndStr($languageCode) . '</time>';
}

/**
Expand All @@ -283,7 +284,7 @@ public function getField($key) {
* <code>false</code> otherwise
*/
public function isPast() {
return $this->endTimestamp < time();
return $this->endTimestamp < new \DateTime("now");
}

/**
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ Returns the [PHP date array](http://php.net/manual/en/function.getdate.php) of t

Returns the formatted string of the beginning of this event. Formatting is done according to the language configuration of Kirby. If the event was given a time, the `calendar-full-time-format` is used, `calendar-time-format` otherwise.

#### getBeginHtml()
#### getBeginHtml($languageCode = 'en')

Returns the formatted string of the beginning of this event as a `time` element with `datetime` attribute.
Returns the formatted string of the beginning of this event as a `time` element with `datetime` attribute. Formatting is based on the passed language code.

#### getEndTimestamp()

Expand All @@ -209,9 +209,9 @@ Returns the [PHP date array](http://php.net/manual/en/function.getdate.php) of t

Returns the formatted string of the ending of this event. Formatting is done according to the language configuration of Kirby. If the event was given a time, the `calendar-full-time-format` is used, `calendar-time-format` otherwise.

#### getEndHtml()
#### getEndHtml($languageCode = 'en')

Returns the formatted string of the ending of this event as a `time` element with `datetime` attribute.
Returns the formatted string of the ending of this event as a `time` element with `datetime` attribute. Formatting is based on the passed language code.

#### getFieldKeys()

Expand Down
12 changes: 6 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
],
'translations' => [
'de' => [
'calendar-full-time-format' => '%d %X',
'calendar-month-format' => '%B %Y',
'calendar-full-time-format' => 'dd.MM.yyyy HH:mm',
'calendar-month-format' => 'MMMM yyyy',
'calendar-no-entry' => 'Zur Zeit gibt es keine Events.',
'calendar-time-format' => '%d',
'calendar-time-format' => 'HH:mm',
'date' => 'Datum',
'description' => 'Beschreibung',
'title' => 'Titel',
'to' => 'bis',
],
'en' => [
'calendar-full-time-format' => '%d %X',
'calendar-month-format' => '%B %Y',
'calendar-full-time-format' => 'MM/dd/yyyy hh:mma',
'calendar-month-format' => 'MMMM yyyy',
'calendar-no-entry' => 'There currently are no events.',
'calendar-time-format' => '%d',
'calendar-time-format' => 'hh:mma',
'date' => 'Date',
'description' => 'Description',
'title' => 'Title',
Expand Down
7 changes: 5 additions & 2 deletions snippets/calendar-div.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
$tmpDate = getdate(0);
$currentDate = getdate();
if (!isset($languageCode)) {
$languageCode = 'en';
}
?>
<section class="calendar">

Expand All @@ -25,9 +28,9 @@
<?php endif; ?>
<div class="row event<?php e($event->isPast(), ' past'); ?>">
<div class="item date"><?php
echo $event->getBeginHtml();
echo $event->getBeginHtml($languageCode);
if ($event->hasEnd()) {
echo ' '.t('to').' '.$event->getEndHtml();
echo ' '.t('to').' '.$event->getEndHtml($languageCode);
}
?></div>
<?php foreach ($fields as $key => $value): ?>
Expand Down
7 changes: 5 additions & 2 deletions snippets/calendar-table.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
$tmpDate = getdate(0);
$currentDate = getdate();
if (!isset($languageCode)) {
$languageCode = 'en';
}
?>

<table class="calendar">
Expand Down Expand Up @@ -30,9 +33,9 @@
<?php endif; ?>
<tr class="event<?php e($event->isPast(), ' past'); ?>">
<td><?php
echo $event->getBeginHtml();
echo $event->getBeginHtml($languageCode);
if ($event->hasEnd()) {
echo ' '.t('to').' '.$event->getEndHtml();
echo ' '.t('to').' '.$event->getEndHtml($languageCode);
}
?></td>
<?php foreach ($fields as $key => $value): ?>
Expand Down
7 changes: 5 additions & 2 deletions snippets/calendar-teaser.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<ul class="teaser">
<?php
if (!isset($languageCode)) {
$languageCode = 'en';
}
foreach ($calendar->getEvents() as $event):
if (--$items < 0) break;
?>
<li><strong><?php echo $event->getBeginHtml(); ?></strong><?php
<li><strong><?php echo $event->getBeginHtml($languageCode); ?></strong><?php
foreach ($fields as $key => $value) {
echo ' '.$event->getField($key);
}
?></li>
<?php endforeach; ?>
</ul>
</ul>

0 comments on commit 56340ae

Please sign in to comment.