简体   繁体   English

在php中本地化时间-Kohana

[英]localize the time in php - Kohana

I am using php and kohana framework.Below is the php code i am using to show the date and time. 我正在使用php和kohana framework。以下是我用来显示日期和时间的php代码。

$message_date = date('Y-m-d', strtotime($message->last_modified_date));
 $current_date = date('Y-m-d');
 $yesterday = date("Y-m-d", strtotime("yesterday"));
 if ($message_date == $current_date):
 $date = "Today at " . date('h:i A', strtotime($message->last_modified_date));
 elseif ($message_date == $yesterday):
 $date = "Yesterday";
 else:
 $date = date("M d, Y", strtotime($message->last_modified_date));
 endif; 



<span class="msg-date"><?= $date; ?></span>

Update: my own tried solution 更新:我自己尝试过的解决方案

$tz = new DateTimeZone('America/Chicago');
$message_date = new DateTime($message_date); 
$message_date->setTimeZone($tz);
$messagedate = $message_date->format('Y-m-d H:i:s');//converted time

I need to localize the time depends on the local machine the user is using.If the user viewing my page from "India" the time should adjust with Indian time,if the same page is viewed in USA or UK,the time should adjusted with USA or UK. 我需要对时间进行本地化,具体取决于用户使用的本地计算机。如果用户从“印度”查看我的页面,则时间应与印度时间调整,如果在美国或英国查看同一页面,则时间应与美国或英国。

How can i do this in kohana or php. 我该如何在kohana或php中做到这一点。

You cannot do this server side (ie in PHP). 您不能在服务器端执行此操作(即在PHP中)。 A server can only read limited data from the client, which doesn't include the locale timezone. 服务器只能从客户端读取有限的数据,其中不包括区域时区。

However, you can use a mix of JS and PHP to do this, using an ajax call that sets a session for example - the same as explained in this question 但是,您可以混合使用JS和PHP,例如使用设置会话的ajax调用-与此问题中的解释相同

I believe that in PHP you're only able to use the time on the server-side. 相信在PHP中,您只能在服务器端使用时间。 If you want to get time/data on the client side of the application, you should use JavaScript. 如果要在应用程序的客户端获取时间/数据,则应使用JavaScript。

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10)
minutes = "0" + minutes

document.write("<b>" + hours + ":" + minutes + " " + "</b>")

Someone also struggled with this issue in this thread. 有人也在此线程中为此问题苦苦挣扎。

How can I get the user's local time instead of the server's time? 如何获取用户的本地时间而不是服务器的时间?

As mentioned earlier, you can use a mix of JavaScript and PHP. 如前所述,您可以混合使用JavaScript和PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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