0
$dt = new DateTime('tomorrow 09:00:00');
echo $app->make('helper/form/date_time')->datetime('dt', $dt, false, true, null, 1800, array());

shows the correct date and time only if the system server time is the same as the user's, e.g. localhost. But if the server and user are in different time zones, the $dt at the user end is no longer that.

I also tried:

$dt = $dh->formatDateTime(date("Y/m/d H:i:s"), false, false, 'user');

which shows the current user date and time, but as soon as I change anything in there (like adding a day or time or anything, it goes back to the server's datetime).

For example, the above shows today's 17/08/18 1:00 (correct user time now), but

$dt = $dh->formatDateTime(date("Y/m/d H:i:s", strtotime("tomorrow 9:00")), false, false, 'user');

shows 18/08/18 1:00 which is 9:00 - 10 h for time zone = server time.

How can I show that exact date and time (tomorrow 9:00) as the user's, not as the system's?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
linuxoid
  • 1,415
  • 3
  • 14
  • 32

1 Answers1

0

You can use the following solution:

// frontend
<script> timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; </script>

This will give you the browser's time zone in timezone. You need to pass that to the backend and use it to adjust the time for the user. Below, the value of timezone above has been stored in $timezone.

// backend
$time = new \DateTime('now');
$time->setTimezone(new \DateTimeZone($timezone));
Myq
  • 1
  • 1
TsV
  • 629
  • 4
  • 7
  • concrete5 should have lots of date/time functions, I'd like to use one of them, just don't know which one does what I need – linuxoid Aug 16 '18 at 14:08
  • 3
    Please explain how the code works. This answer is in the low quality queue. – skrx Aug 16 '18 at 16:50