From eac020907afced2c33db53b564a80e4764dfe3b7 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 26 Mar 2025 15:18:32 +0100 Subject: allow 20241212 is second arg of it::date --- it.class | 4 +++- test/it.t | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/it.class b/it.class index b93d49a..a114e35 100644 --- a/it.class +++ b/it.class @@ -1054,12 +1054,14 @@ static function time() * Formats can be qualified with language, e.g. "date:en" * Special formats without language support are "icsdate", "icsdatetime". * @param stamp optional unix timestamp (default is now). - * If it contains nondigits, it is fed to strtotime + * If it contains nondigits or is a plausible 8 digit date YYYYMMDD, it is fed to strtotime */ static function date($format = "", $stamp = null) { if (!isset($stamp)) $stamp = it::time(); + else if (ctype_digit((string)$stamp) && 19000000 < $stamp && $stamp < 21000000) + $stamp = strtotime($stamp); else if (is_string($stamp) && !ctype_digit($stamp)) $stamp = strtotime(it::replace(['\s(--|\+\+)\s*(\d)' => ' +\2', '\s(\+-|-\+)\s*(\d)' => ' -\2'], $stamp), it::time()); diff --git a/test/it.t b/test/it.t index 535f185..6ea1859 100755 --- a/test/it.t +++ b/test/it.t @@ -420,6 +420,10 @@ is(it::date('datetime', 1000000), it::date('datetime', "1000000"), '... large nu is(it::date('datetime', 1000000.543), it::date('datetime', "1000000"), '... large nummer and point'); is(it::date('time', "10.5"), "10:05", 'interpret string with points with strtotime'); is(it::date('time', "10.05"), "10:05", 'interpret string with points with strtotime'); +is(it::date('Y-m-d', "20000101"), '2000-01-01', 'interpret 8 digit strings as dates'); +is(it::date('Y-m-d', 20000101), '2000-01-01', 'interpret 8 numbers strings as dates'); +is(it::date('Y-m-d', "20990101"), '2099-01-01', 'interpret 8 digit strings as dates'); +is(it::date('Y-m-d', strtotime("1980-01-01")), '1980-01-01', 'interpret typical time stamps correctly'); # it::uc* is(it::ucfirst('foo bär über'), 'Foo bär über'); -- cgit v1.2.3