MySQL 日期时间到整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9399346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:14:30  来源:igfitidea点击:

Datetime to Integer

mysqldatetime

提问by foxns7

I have one problem... There is a table in my MySQL database that stores to-do list entries inside a JQuery-type calendar.
I had to generate a calendar_idthat will generate a reminder once I created timestamp (considered as time I clicked on any of the calender dateboxes, to key in some to-do tasks - put it simple: created datetime).

我有一个问题...我的 MySQL 数据库中有一个表,用于在 JQuery 类型的日历中存储待办事项列表条目。一旦我创建了时间戳,
我必须生成一个calendar_id会生成提醒的时间(被视为我点击任何日历日期框的时间,以键入一些待办事项 - 简单地说:创建日期时间)。

This to-do list activities app is an external application that I've been working on to integrate with my own management system. I noticed that,the timestamp column is in int(11)format, so whatever timestamp entered will be converted into integer.

这个待办事项列表活动应用程序是我一直致力于与我自己的管理系统集成的外部应用程序。我注意到,时间戳列是int(11)格式,所以输入的任何时间戳都将转换为整数。

For example, take a look at this:

例如,看看这个:

2012-02-22 15:31:24

converted to

转换成

1329899400

How can we convert datetime to this format? It's not in seconds when I tried:

我们如何将日期时间转换为这种格式?当我尝试时,它不是在几秒钟内:

intval(floor($datetime/86400));

Any help?

有什么帮助吗?

回答by Chris

FROM UNIXTIMEcan format UNIX timestamps into datetime fields:

FROM UNIXTIME可以将 UNIX 时间戳格式化为日期时间字段:

SELECT FROM_UNIXTIME(time)
FROM ...

The reverse function would be UNIX_TIMESTAMP.

反向功能是UNIX_TIMESTAMP



Alternatively you can do it in PHP, if available:
To store a date into the DB format it like this:

或者,您可以在 PHP 中执行此操作(如果可用):
要将日期存储为 DB 格式,如下所示:

$datetimeStr = '2012-02-22 15:31:24';
$datetime = strtotime($datetimeStr);

To retrieve it from the DB and format it to the original format, use something like this:

要从数据库中检索它并将其格式化为原始格式,请使用以下内容:

$dateTimeFromDB = '1329921084';
$datetimeStr = date('Y-m-d H:i:s', $dateTimeFromDB);

回答by Mchl

Here's a nice MySQL's UNIX_TIMESTAMP()function for you

这是一个不错的 MySQLUNIX_TIMESTAMP()函数给你