如何以本地时间格式显示Unix时间 [英] How to show a Unix-time in a local time format

查看:133
本文介绍了如何以本地时间格式显示Unix时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php变量说 $ expTime (其中有一个unixtime说-1359683953)。我想在客户端显示此变量(根据当地时间以适当的时间格式)。我在UTC,GMT,DST之间这么困惑的事情。任何人都可以使用php或javascript来提供解决方案。

I have a php variable say $expTime (which has a unixtime say-1359683953) . I want to display this variable on the client side(in a proper time format according to his local time) . I am so confused between the UTC ,GMT , DST all that things. Can anyone suggest a solution for this using php or javascript please.

当我使用 echo date('h:i M d / y' ,$ expTime)它显示错误的时间。

when I am using echo date('h:i M d/y',$expTime) it is showing me a wrong time.

我如何保存时间到数据库:

var exp_day = parseInt($(#exp_day)。val());

var exp_hrs = parseInt ($(#exp_hrs)。val());

var exp_min = parseInt($(#exp_min)。val ));

var exp_time =(exp_day * 24 * 60 * 60)+(exp_hrs * 60 * 60)+(exp_min * 60) ;

然后我发布了exp_time使用ajax到php文件 -

$ expTime = time()+ $ _POST [exp_time];

How I am saving the time to database:
var exp_day= parseInt($("#exp_day").val());
var exp_hrs= parseInt($("#exp_hrs").val());
var exp_min= parseInt($("#exp_min").val());
var exp_time = (exp_day*24*60*60) + (exp_hrs*60*60) + (exp_min*60) ;
then I posted the exp_time using ajax to a php file -
$expTime = time() + $_POST["exp_time"];

从数据库检索的是$ expTime。这个$ expTime我想根据当地的时区(也是确保日光节省)显示在所有的客户端系统上。

What I am retrieving from the database is $expTime . This $expTime I want to display it on the all the clients system according to there local time zone (also by making sure the day light saving)

推荐答案

UNIX时间值通常是从时代开始的UTC秒。 Javascript 时间值是自同一时代以来的UTC毫秒。 ECMA-262 Date.prototype.toString 方法自动生成表示本地时间的字符串,并将其应用于夏令时。

UNIX time values are usually UTC seconds since epoch. Javascript time values are UTC milliseconds since the same epoch. The ECMA-262 Date.prototype.toString method automatically generates a string representing the local time and takes account of daylight saving if it applies.

您还可以使用 Date 方法创建您自己格式化的字符串,它们还返回本地的时间和日期值。如果要在UTC中工作,也可以使用UTC方法。

You can also use Date methods to create your own formatted string, they also return local time and date values. There are also UTC methods if you want to work in UTC.

要在客户端上执行此操作,只需从服务器提供UTC时间值,然后在javascript是:

To do this on the client, just provide a UTC time value from the server and then all you need in javascript is:

var timeValue = 1359683953;  // assume seconds
var date = new Date(timeValue * 1000);  // convert time value to milliseconds

alert(date); //  Fri 01 Feb 2013 11:59:13 GMT+1000 

这篇关于如何以本地时间格式显示Unix时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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