根据从UI中选择的时区在php中过滤日期 [英] filter dates in php in accordance with timezone selected from UI

查看:86
本文介绍了根据从UI中选择的时区在php中过滤日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个如下所示的php代码,其中包含air_date,当我从UI 中选择不同的时区时,我想要更改

I am working on a php code as shown below which has air_date which I want to be changed when different timezone is selected from UI.

Php代码:

$cols = array(
    'e.description_' . $opp_lang . ' as translation',
    's.air_date',  // This is the entry point of air_date and its been used everywhere. 
    'e.program_id',
); 

我用于时区的逻辑:

function tz_translations( $lang = 'en' ) {
    $tz_translations = [
        'en' => [
            'PST' => [
                'label' => 'PT',
                'diff'  => 'subtract 3 hours',
            ],
            'EST' => [
                'label' => 'ET',
                'diff'  => 'subtract 0 hours',
            ],
        ],
        'fr' => [
            'HNP' => [
                'label' => 'HP',
                'diff'  => 'subtract 3 hours',
            ],
            'HNE' => [
                'label' => 'HE',
                'diff'  => 'subtract 0 hours',
            ],
        ],
    ];

    return $tz_translations[ $lang ];
}   

UI (JS代码):

UI (JS Code):

<ul id="js-timezone-picker">
   <?php foreach ( $tz_translations as $key => $tz ) :
      $tz_param = strtolower( $tz['label'] );
      $active = ( $current_tz === $key ) ? 'active' : '';
      ?>
   <li>
      <button id="<?php echo esc_attr( 'js-time-' . $tz_param ) ?>"
         class="<?php echo sanitize_html_class( $active ) ?>"
         data-timezone="<?php echo esc_attr( $tz_param ) ?>"><?php echo esc_html( $tz['label'] ) ?></button>
   </li>
   <?php endforeach; ?>
</ul>

选择PT / ET后,检查时会显示以下内容:

When PT/ET is selected then it becomes the following on inspect:

<button id="js-time-pt" class="" data-timezone="pt">PT</button>
<button id="js-time-et" class="" data-timezone="et">ET</button>

问题陈述:

我想知道我应该在上面的 php代码中做出哪些更改,这样当从UI / JS代码中选择不同的时区时, php代码中的air_date 会根据时区自动更改。

I am wondering what changes I should make in the php code above so that when different time-zones is selected from the UI/JS code, air_date in the php code gets changed automatically in accordance with time-zones.

推荐答案

你的问题并不容易回答。我不清楚你的代码如何挂在一起,或者它们究竟代表什么。我认为最好从一些一般的实际定义开始。

Your question is not easy to answer. It unclear, to me, how your pieces of code hang together, or what they exactly represent. I think it therefore is best to start with some general practical definitions.

'内部时区是您在PHP代码,数据库,服务器等中使用的时区。到处。你需要选择一个。最合乎逻辑的选择是UTC,但任何其他区域都可以,只要你坚持下去。拥有一个内部时区意味着您永远不必从一个区域转换到另一个区域。

The 'Internal time zone' is the time zone used by you, inside your PHP code, your database, your server, and so on. Everywhere. You need to choose one. The most logical choice is UTC, but any other zone will do, as long as you stick to it. Having one internal time zone means you never have to do any conversions from one zone to another.

外部时区是您用来向访问者显示日期和时间的时区。正如您所假设的那样,外部时间是您内部时间的一些差异。此转换应由您的应用程序的表示层进行。因此,只有在最晚时刻才能将内部时间转换为外部时间。

The 'External time zone' is the time zone you use to present dates and times to your visitors. As you rightly assumed, the external time is a translation, by some difference, of your internal time. This conversion should be made by the presentation layer of your application. So only at the latest moment possible will the internal time be converted to an external time.

您可以在Javascript中将时间从一个区域转换为另一个区域,但为什么会这样?时间应由PHP提供,使用服务器时间或数据库中的字段,因此使用PHP进行从内部到外部的转换是有意义的。

You could convert time from one zone to another in Javascript, but why would you? The time should be supplied by PHP, either using server time, or a field from a database, so it would make sense to use PHP to do the conversion from internal to external time.

这意味着您需要将用户选择的时区存储在可以从PHP中轻松访问的时区。最常见的方法是将其存储在会话中。这通常通过从Javascript进行AJAX调用到PHP后端来完成。 JQuery 可以轻松使用AJAX。

This implies you need to store the time zone, selected by the user with your nice UI, in a way that it is easily accessible from within PHP. The most common way to do this is to store it in a session. This is normally done with an AJAX call from Javascript to the PHP back-end. JQuery can make using AJAX easy.

如评论中所述其他人,最好使用现有的时区值。当然,仅供内部使用。您如何向用户提供时区取决于您。

As said in comments by others, it's best to use existing time zone values. For internal use only, of course. How you present a time zone to your users is up to you.

所以让我们进入一些代码。我们假设内部时间是UTC,你有一个内部时间字符串,例如来自你的数据库:

So let's get into a bit of code. Let us assume the internal time is UTC and you have an internal time string, for instance from your database:

$internalTimeStr = '2019-04-12 13:33:20';

很容易将其转换为PHP可以使用的时间:

It's easy to convert this to a time that PHP can use:

$internalTime = strtotime($internalTimeStr);

反之亦然:

$internalTimeStr = date('Y-m-d H:i:s', $internalTime);

现在假设您网站的访问者在UI中选择了America / New_York来代表东部时间。您可以使用此选项将内部时间转换为外部时间。

Now suppose the visitor of your site has selected 'America/New_York' in the UI to represent Eastern Time. You can use this selection to convert the internal time to the external time.

function utc2local($utc,$localTimezone)
{
    $dt = new DateTime($utc);
    $dt->setTimezone(timezone_open($localTimezone));
    return $td->getTimestamp();
}

// we know this
$externalTimezone = 'America/New_York';     // from session / UI
$internalTimeStr  = '2019-04-12 13:33:20';  // from database / system
// and this is the conversion
$internalTime     = strtotime($internalTimeStr);
$externalTime     = utc2local($internalTime,$externalTimezone);
$externalTimeStr  = date('Y-m-d H:i:s', $externalTime);

重要的是要意识到这种转换可以通过多种方式实现,而这只是一个例。在使用之前确保代码符合您的要求。

It is important to realize that this conversion can be achieved in many ways, and that this is just one example. Make sure the code does what you want, before you use it.

使用PHP构建的函数的原因是时间困难真的很难

The reason to use the functions build into PHP is that time is difficult, really difficult.

现在,您可以使用此外部时间向访问者显示时间。

Now you can use this external time to show the time to your visitors.

这可能不完全符合您的预期,但我希望它能为您提供一些好的指示。使用会话和AJAX调用是你必须自己找到的东西,进入那个问题超出了这个问题的范围。如果您有任何具体问题,可以随时提出另一个问题。但是试着在一个问题中处理一个特定问题。

This might not be completely what you expected, but I hope it gives you some good pointers. Using sessions and AJAX calls is something you have to find out yourself, it would be beyond the scope of this question to go into that. If you have any specific problems with it, you can always ask another question. But try to handle one specific problem in one question.

这篇关于根据从UI中选择的时区在php中过滤日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆