php utc时间_PHP时间戳UTC

I have a PHP MySQL query that inserts some data into a MySQL database and it includes a timestamp.

Currently the INSERT query uses NOW() for the the timestamp column and it is saved in the database in the following format: 2012-07-24 13:13:02

Unfortunately for me the Server is not in my time zone and it is listed as America/Los_Angeles as shown print date_default_timezone_get();

I was hoping to do the following:

date_default_timezone_set('Europe/London');

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

and simply save into the database the $timefordbLondonEU in place of the NOW();

Is this a good way to save such data ?

Many Thanks,

Richard

[ADDED TEXT]

I changed the Type in the MySQL db to DateTime and did the following:

date_default_timezone_set('Europe/London');

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

It is working but Im still not getting the overall concept yet.

Assumptions based on your comments:

MySQL = Does not have a datatype UTC you simply use type INT.

Unix_TimeStamp() will save the current time or count? in UTC format such as 1343247227.

As UTC is a count from a common 0 point you can get any timezone from it. Assuming that you don't want a date before the reference 0 point in 1970.

My guess and lead on from what you have said is the best way to do it is save the time as UTC in an INT (1343247227) and then generate any time zones you want from there. Again assuming you don't need to store dates before the reference 0 point in 1970.

Equally why not store as datetime YYYY-MM-DD HH:MM:SS at a known timezone and then convert to UTC or other timezones. It all seems pretty messy =(

解决方案

As @Petah said in the comments, store your times in UTC and covert them in the application as needed.

Unix timestamps are in UTC so I usually store my times in the database as timestamps. This saves the headache and confusion of first converting to UTC to insert, and then from UTC when selecting.

That is, make your time field an INT type, and use the function UNIX_TIMESTAMP() in MySQL when you insert, or get the timestamp from PHP using the time() function.

When you fetch the timestamp from the DB it will be in UTC, but when you display it in your PHP application using date(), it will display in the server timezone, or whatever you set with date_default_timezone_set.

Therefore the following two queries will work:

INSERT INTO `table` (id, time) VALUES(NULL, UNIX_TIMESTAMP());

// or

$time = time();

$query = "INSERT INTO `table` (id, time) VALUES(NULL, $time);

If you want to select it from the DB as a DATETIME, you can do this:

SELECT *, FROM_UNIXTIME(time) as dt FROM `table` WHERE 1

The resulting dt column will be in the format yyyy-MM-dd hh:mm:ss.

You can format the numeric timestamp in PHP using date()

If the PHP version you have is 64-bit, you aren't limited to the 1970 - 2036 range, PHP will support 64-bit timestamps, just make sure to use a BIGINT column in MySQL in that case.

Hope that helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值