简体   繁体   中英

display the server timezone datetime object according to the local timezone.datetime object on listing | DateTime object | Moment.js

I have an application(built using core PHP) and I want to show the correct DateTime of the post according to the local timezone.

In my current application, a user can be able to select the publish date of the post by using the bootstrap DateTime picker(which automatically picks the user's system timezone, which is correct) and my current server timezone is "UTC".

when a user creates a post and selects the publish date time so that the user can select the date-time according to their local timezone(eg., "Asia/Kolkata") but the value should be saved according to the server timezone.

To save the data into the database I do the following I create a hidden type element in add form

<input type="hidden" name="client_tz" value="">

and using moment.js place the value on the document load like this

$(document).ready(function () {
    $("input[name='client_tz']").val(moment.tz.guess());
});

and on the server-side, I do the following to convert the value to the server timezone

$postParams = $_POST;
$date = new DateTime($postParams['date'], new DateTimeZone($postParams['client_tz']));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$postParams['date'] = $date->format('Y-m-d H:i:s');

On the listing page(a very long list), in data, the date field is coming in "UTC" which is correct but I want to display the note's date according to the local timezone. and obvious local time zone can be anything.

The problem is the website is built using the core PHP and I don't find a correct way to apply third party(moment.js) transformation of the server date-time object to the local timezone DateTime object and also If I want to perform this transformation on the server side then I don't know what is the local timezone.

Use case:- For eg., the current time of UTC is 7:10 am Tuesday, 6 October 2020, and the current time of Asia/Kolkata is 12:40 pm Tuesday, 6 October 2020 (IST) So the value is saved as "2020-10-07 07:10:00" and also display as "07-Oct-2020 07:10 AM" but it should be displayed as "07-Oct-2020 12:40 PM"

How the value save in DB数据库中的注释

How the value is displayed笔记列表

In mysql you can convert from one timezone to another like:

select convert_tz(yourcolumn, 'America/Los_Angeles', 'Asia/Kolkata') from yourtable

or from UTC like:

select convert_tz(yourcolumn, '+00:00', 'Asia/Kolkata') from yourtable

Verify that your database server has timezone information loaded:

select * from mysql.time_zone_name where Name='Asia/Kolkata';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM