C5-8.3.2: how to handle DateTime between versions 8.0.0 and 8.3.2?

Permalink
The translate() method of the date_time helper returns a date-time string which is saved in the database:
public function save($args) 
    {
        $args['valid_until'] = $this->app->make('helper/form/date_time')->translate('valid_until', null);
        parent::save($args);
    }

http://documentation.concrete5.org/api/8.3.2/Concrete/Core/Form/Ser...

The problem is the time is saved in v8.0.0 based on the UTC but it's saved in v8.3.2 based on the user time.

To develop a package which will run without errors from Concrete5 versions 8.0.0 to 8.3.2, how can I make sure the date-time string is always displayed in the correct user time zone? How can I handle the date-time string saving and view so that they are correct regardless of the Concrete5 version? Is it possible at all?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
My hosting site is set to Russian language, the hosting provider is in UTC+3 time zone, I'm in a UTC+11 time zone. I need to get 2 datetime strings: one in the format set in the C5 Time Zone and Localization setting, and one in this exact format 'Y/m/d H:i:s'.

I am selecting 2018-05-24 23:00 with the DateTime picker, 2018-05-24 15:00:00 is saved in the database.

I tried the following:
echo 'dt_app: ' . $dh->formatDateTime($valid_until, true, true, 'app') . '<br />';
echo 'dt_user: ' . $dh->formatDateTime($valid_until, true, true, 'user') . '<br />';
echo 'dt_system: ' . $dh->formatDateTime($valid_until, true, true, 'system') . '<br />';
echo 'dt_formated_app: ' . $dh->formatDateTime(date(t('Y/m/d H:i:s'), strtotime($valid_until)), true, true, 'app') . '<br />';
echo 'dt_formated_user: ' . $dh->formatDateTime(date(t('Y/m/d H:i:s'), strtotime($valid_until)), true, true, 'user') . '<br />';
echo 'dt_formated_system: ' . $dh->formatDateTime(date(t('Y/m/d H:i:s'), strtotime($valid_until)), true, true, 'system') . '<br />';
echo 'vu: ' . $valid_until . '<br />';
echo 'vu_formated: ' . date("Y/m/d H:i:s", strtotime($valid_until)) . '<br />';

and I get the following result:
dt_app: 24 мая 2018 г., 23:00:00
dt_user: 24 мая 2018 г., 23:00:00
dt_system: 24 мая 2018 г., 15:00:00
dt_formated_app: 24 мая 2018 г., 23:00:00
dt_formated_user: 24 мая 2018 г., 23:00:00
dt_formated_system: 24 мая 2018 г., 15:00:00
vu: 2018-05-24 15:00:00
vu_formated: 2018/05/24 15:00:00

As you can see results No 1, 2, 4, 5 are what I need for the first string. The last result has the right date format but wrong time, it must be 23:00. If I get the right time format above 23:00, the date is in Russian, it must simply be 2018/05/24.

What else can I try?
linuxoid replied on at Permalink Reply
linuxoid
The following seems to work:
$dh = $app->make('helper/date');
$fdt = $dh->formatDateTime($valid_until, true, true, 'user');
$fc = $dh->formatCustom("Y/m/d H:i:s", $valid_until, 'user', 'system');
if (version_compare(\Config::get('concrete.version_installed'), '8.1.0', '<')) {
    $td = strtotime($fc) - time();
}
else {
    $td = strtotime($valid_until) - time();
}

with the following test passing with different time zones on different servers:
echo $valid_until . '<br />';
echo date("Y/m/d H:i:s", strtotime($valid_until)) . '<br />';
echo $valid_until . '<br />';
echo $fdt . '<br />';
echo $fc . '<br />';
echo date("Y/m/d H:i:s", time()) . '<br />';
echo $td/60 . '<br />';

Result:
2018-05-24 14:00:00
2018/05/24 14:00:00
2018-05-24 14:00:00
May 25, 2018, 1:00:00 AM
2018/05/25 01:00:00
2018/05/25 00:17:45
42.25