filter dates in php in accordance with timezone selected from UI

被刻印的时光 ゝ 提交于 2019-12-23 09:33:13

问题


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 code:

$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',
); 

Logic I have used for timezone:

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 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>

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>

Problem Statement:

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.


回答1:


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.

Internal time zone

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.

External time zone

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.

Conversion with 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.

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.

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';

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

$internalTime = strtotime($internalTimeStr);

And the other way around:

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

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.

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.

Conclusion

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.




回答2:


If it is just a conversion and you don't need other server info you should pass all data to client-side (e.g. By json format), and then with the button, trigger a Javascript function that does your specific conversion. But if you need to change php objects you need a server request (get, post or Ajax).



来源:https://stackoverflow.com/questions/55525527/filter-dates-in-php-in-accordance-with-timezone-selected-from-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!