PHP:根据用户的时区显示时间 [英] PHP: Show time based on user's timezone

查看:71
本文介绍了PHP:根据用户的时区显示时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法在 php 中获取用户的时区,然后根据查看者的时区显示时间.我知道它可以在 javascript 中完成,但我想要一个在 php 中的解决方案.我搜索了很多,但找不到获得这项工作的方法.

I wonder if there is a way to get the timezone of the user in php and then display a time based on viewer's timezone. I know it can be done in javascript but i want a solution in php. I searched a lot but can't find a way to get this work.

所以基本上假设时间是 8:30 GMT+0530.它在印度时区,如果用户在纽约查看它,那么它应该是 21:30.

So basically suppose the time is 8:30 GMT+0530. Its in indian timezone i want if a user views this in Ne York then it should be 21:30 .

推荐答案

如果您在注册时添加了用户 Timezone 或者您以某种方式知道用户 Timezone,那么您可以试试这个...

If you added the users Timezone while registering or you know users Timezone somehow, then you can try this...

$users = [
    [
        'username' => 'foo',
        'timezone' => 'America/Los_Angeles'
    ],
    [
        'username' => 'bar',
        'timezone' => 'America/New_York'
    ]
];

foreach ($users as $user) {
    date_default_timezone_set($user['timezone']);
    echo $user['username'] . ' date and time is ' . date('Y-m-d H:i:s') . '<br />';
}

输出

foo time is 2013-02-28 18:13:49
bar time is 2013-02-28 21:13:49

您还可以在 JavaScrpt 中使用时区 在 JavaScript 中获取客户端的时区.发出 Ajax 请求并保存在 PHP 会话或数据库中.

You can also user Timezone in JavaScrpt Getting the client's timezone in JavaScript. Make an Ajax request and Save in PHP Session or database.

这篇关于PHP:根据用户的时区显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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