简体   繁体   English

如何将datetime属性转换为unix timestamp属性?

[英]How to convert datetime attribute into unix timestamp attribute?

I want to upgrade my database table by changing the datatype of the column "date_added" from 我想通过从以下位置更改“ date_added”列的数据类型来升级数据库表

`date_added` datetime DEFAULT NULL

to

`new_date_added` bigint(20) NOT NULL

date_added has datetime values stored in the format "YYYY-MM-DD HH:MM:SS". date_added具有以“ YYYY-MM-DD HH:MM:SS”格式存储的日期时间值。 This database table has more than 15000 rows. 该数据库表具有15000多行。 What is the best way to perform this modification? 进行此修改的最佳方法是什么?

Note: I have tried executing following MySql query : 注意:我尝试执行以下MySql查询:

ALTER TABLE  `my_table` ADD  `new_date_added` bigint(20) NOT NULL AFTER  `date_added`;
UPDATE `my_table` SET new_date_added = UNIX_TIMESTAMP(  `date_added` );
ALTER TABLE  `my_table` DROP `date_added`;

Above solution doesn't convert datetime value into correct unix_timestamp. 上述解决方案无法将datetime值转换为正确的unix_timestamp。 If I compare UNIX timestamp created after the modification with its original datetime value, there is a diffrence of 19800 seconds. 如果我将修改后创建的UNIX时间戳与其原始datetime值进行比较,则相差19800秒。

19800 seconds = 5:30 hours. 19800秒= 5:30小时 You are probably located in India (GMT + 5:30). 您可能位于印度(格林尼治标准时间+ 5:30)。

UNIX_TIMESTAMP() generates timestamps that are relative to GMT , so it is likely that your values are correct. UNIX_TIMESTAMP()生成相对于GMT的时间戳,因此您的值很可能是正确的。

To get the right value back, you could eg use PHP's date() and set the correct timezone. 为了获得正确的值,您可以例如使用PHP的date()设置正确的时区。

Make sure, however, that you have a really good reason to move away from DATETIME in the first place. 但是,请确保您确实有充分的理由首先离开DATETIME。 It's generally the superior format, allows for all kinds of optimized operations within mySQL, and is free from the year 2038 problem that 32-bit timestamps have. 它通常是高级格式,可以在mySQL中进行各种优化的操作,并且没有20位时间戳在2038年出现的问题

I found the solution for my problem. 我找到了解决我问题的方法。 Following is the final working code which is giving me correct timestamps : 以下是最终的工作代码,为我提供了正确的时间戳:

SET time_zone = "+00:00";
ALTER TABLE  `my_table` ADD  `new_date_added` bigint(20) NOT NULL AFTER  `date_added`;
UPDATE `my_table` SET new_date_added = UNIX_TIMESTAMP(  `date_added` );
ALTER TABLE  `my_table` DROP `date_added`;

@Ela Buwa: I have written the exact same code which is giving me an incorrect timestamp values (due to automatically adjusted/shifted timestamp values) @Ela Buwa:我已经编写了完全相同的代码,这给了我错误的时间戳值(由于自动调整/移动了时间戳值)

@Pekka: My question was about mysql's UNIX_TIMESTAMP() giving me unexpected values. @Pekka:我的问题是关于mysql的UNIX_TIMESTAMP()给我意外的值。 Using PHP's date() function will not affect mysql's timezone settings. 使用PHP的date()函数不会影响mysql的时区设置。 That is why i used SET time_zone = "+00:00"; 这就是为什么我使用SET time_zone = "+00:00"; to configure mysql's timezone. 配置mysql的时区。 Thank you for taking a time to answer my question though. 谢谢您花时间回答我的问题。

Thank you all for the efforts and sparing your valuable time. 感谢大家的努力,并节省您宝贵的时间。

I'd use strtotime. 我会用strtotime。 It says the time in the number of seconds passed since 1970-01-01 00:00:00 它表示从1970-01-01 00:00:00起经过的时间(以秒为单位)

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("3 October 2005") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday"));
?>

If you want to do it with in mysql, use UPDATE table_name SET timefield=UNIX_TIMESTAMP(timefield); 如果要在mysql中执行此操作,请使用UPDATE table_name SET timefield = UNIX_TIMESTAMP(timefield); I haven't tested it out though. 我还没有测试过。 You might want to check the mysql documents here. 您可能要在此处检查mysql文档。 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

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

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