将服务器MySQL时间戳转换为UTC [英] Converting Server MySQL TimeStamp To UTC

查看:446
本文介绍了将服务器MySQL时间戳转换为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用timeago插件(http://timeago.yarp.com/),它在我的本地主机上运行良好,该主机的MySQL将其数据存储在UTC时间戳中,该插件需要工作.

I've been using the timeago plugin (http://timeago.yarp.com/), it has been working fine on my localhost, which has its MySQL storing its data in the UTC timestamp, which the plugin needs to work.

但是,将我的项目上载到服务器时,MySQL数据库中出现了另一种类型的时间戳.我从服务器获取了这样的时间戳:"2011年2月24日,星期四,下午4:29",而我需要这样的时间戳:"2008-07-17T09:24:17Z"

However, when uploading my project to the server, a different type of timestamp is appearing in it MySQL database. I'm getting a timestamp like this: "Thursday, February 24, 2011 4:29 PM" from the server, whereas I need something like this: "2008-07-17T09:24:17Z"

有什么想法如何使用php转换时间戳吗?

Any idea how to convert the timestamps using php?

以错误的格式存储在数据库中的时间戳是由mysql自动生成的.

The timestamps stored in the wrong format in the database are automatically generated by mysql.

这是类型为时间戳"的字段,当在db中插入行时,默认设置为"CURRENT_TIMESTAMP"

Edit 2: It's a field of type "timestamp" and default set to "CURRENT_TIMESTAMP" when row is being inserted in db

推荐答案

您正在获取MySQL的奇怪字符串,确定它在Datetime字段中吗?

You are getting a weird string for MySQL, are you sure that it is in a Datetime field?

您可以使用以下

You can get a UNIX timestamp (seconds since epoch) from MySQL with the following function, this format is widely accepted over multiple platforms:

SELECT UNIX_TIMESTAMP( table.datetime_field ) as datetime_field FROM table

使用一些 PHP函数,您可以将其转换为所需的格式:

Using some PHP Functions you can convert this to the format you desire:

echo date( 'c', $record[ 'datetime_field' ] );

我认为这足以解决您的问题.

I think this would be sufficient for your problem.

这篇关于将服务器MySQL时间戳转换为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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