Skip to content
Snippets Groups Projects
Commit d2b0935a authored by Torsten Kunst's avatar Torsten Kunst
Browse files

refs #54872 - refactored formatting datetime to local dateformats and add basic unittests

parent d84055ed
No related branches found
No related tags found
No related merge requests found
<?php
namespace BO\Dldb\Helper;
class DateTime extends \DateTimeImmutable
{
public static function getFormatedDates(
$timestamp,
$pattern = 'MMMM',
$locale = 'de_DE',
$timezone = 'Europe/Berlin'
) {
$dateFormatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
$timezone,
\IntlDateFormatter::GREGORIAN,
$pattern
);
return $dateFormatter->format($timestamp);
}
public function __toString()
{
return $this->format('c');
}
}
......@@ -167,15 +167,25 @@ class TwigExtension extends \Twig_Extension
return $date;
}
public function formatDateTime($datetime)
public function formatDateTime($dateString)
{
$formatDate['date'] = strftime('%a, %d. %B %Y', $datetime);
$formatDate['fulldate'] = strftime('%A, den %d. %B %Y', $datetime);
$formatDate['weekday'] = (date('w', $datetime) == 0) ? date('w', $datetime) + 6 : date('w', $datetime) - 1;
$formatDate['time'] = (date('H:i', $datetime) != '00:00') ? strftime('%H:%M Uhr', $datetime): false;
$formatDate['dateId'] = date('Y-m-d', $datetime);
$formatDate['ym'] = date('Y-m', $datetime);
$formatDate['timeId'] = (date('H:i', $datetime) != '00:00') ? date('H:i', $datetime) : false;
$dateTime = new \DateTimeImmutable($dateString, new \DateTimezone('Europe/Berlin'));
$formatDate['date'] = Helper\DateTime::getFormatedDates($dateTime, "EE, dd. MMMM yyyy");
$formatDate['fulldate'] = Helper\DateTime::getFormatedDates($dateTime, "EEEE, 'den' dd. MMMM yyyy");
$formatDate['weekday'] = ($dateTime->format('N') == 0) ?
$dateTime->format('N') + 6 :
$dateTime->format('N') - 1;
$formatDate['weekdayfull'] = Helper\DateTime::getFormatedDates($dateTime, "EEEE");
$time = $dateTime->format('H:i');
$formatDate['time'] = ($time != '00:00') ?
Helper\DateTime::getFormatedDates($dateTime, "HH:mm 'Uhr'") :
false;
$formatDate['timeId'] = ($time != '00:00') ? $time : false;
$formatDate['ts'] = $dateTime->getTimestamp();
$formatDate['dateId'] = $dateTime->format('Y-m-d');
$formatDate['ym'] = $dateTime->format('Y-m');
return $formatDate;
}
......
<?php
namespace BO\Dldb\Tests;
use PHPUnit\Framework\TestCase;
class DateTimeFormatTest extends TestCase
{
public function testTwigDateFormat()
{
$twigExtensionsClass = new \BO\Dldb\TwigExtension();
$date = '2016-04-01';
$this->assertEquals('2016-04-01', $twigExtensionsClass->formatDateTime($date)['dateId']);
$this->assertEquals('Fr., 01. April 2016', $twigExtensionsClass->formatDateTime($date)['date']);
$this->assertEquals('Freitag, den 01. April 2016', $twigExtensionsClass->formatDateTime($date)['fulldate']);
$this->assertEquals('1459461600', $twigExtensionsClass->formatDateTime($date)['ts']);
$this->assertEquals(4, $twigExtensionsClass->formatDateTime($date)['weekday']);
$this->assertEquals('Freitag', $twigExtensionsClass->formatDateTime($date)['weekdayfull']);
$this->assertEquals('11:55 Uhr', $twigExtensionsClass->formatDateTime($date . ' 11:55:00')['time']);
$this->assertEquals('11:55', $twigExtensionsClass->formatDateTime($date . ' 11:55:00')['timeId']);
}
}
<?php
namespace BO\Dldb\Tests;
use PHPUnit\Framework\TestCase;
class TwigExtensionTest extends TestCase
{
public function testBasic()
{
$twigExtensionsClass = new \BO\Dldb\TwigExtension();
$this->assertEquals('dldb', $twigExtensionsClass->getName());
$this->assertEquals('<pre>unittest</pre>', $twigExtensionsClass->dump('unittest'));
$this->assertEquals('Montag', $twigExtensionsClass->convertOpeningTimes('monday'));
$this->assertEquals('1459511700', $twigExtensionsClass->dateToTS('2016-04-01 11:55'));
$this->assertEquals('2016-04-01', $twigExtensionsClass->tsToDate('1459511700'));
$this->assertEquals('ec', $twigExtensionsClass->kindOfPayment(2));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.