Compare Dates (UTC with Local)


#1

I have got a problem comparing two dates:

First date (see debug upper) is the output of Time::now();
Second date (see debug lower) is the output of a database field.

Now if i check, if the second date “->isPast()” it returns false. I think this happens because of the time difference between UTC and Europe/Vienna (+02:00 i guess?).

I tried to set the timezone at the datasource setting -> “+02:00”, but it makes no difference.

Any suggestions to fix this?

regards,


#2

To make the camparison I would take the euro/vienna timestamp convert it to UTC and then compare. Does that help? Not sure what you mean by at “datasource”


#3

well, actually both are Europe/Vienna Timestamps … if i’d convert the first to UTC, the time would be 09:14:29 … i just want to compare 11:14:29 and 10:50.05 … but it wont work :sweat:


#4

any one else an idea?


#5

try to set cake to work with your locale/timezone:

in config/app.php:
    'App' => [
                'defaultLocale' => env('APP_DEFAULT_LOCALE', 'UR-LOCALE-CODE')

in config/bootstrap.php:

date_default_timezone_set('Europe/Vienna');
\Cake\I18n\I18n::locale('UR-LOCALE-CODE');

Type::build('time')->useImmutable();
Type::build('date')->useImmutable()->useLocaleParser();
Type::build('datetime')->useImmutable()->useLocaleParser();
Type::build('timestamp')->useImmutable();

now both dates should be in the same timezone and you may proced the compare