简体   繁体   English

unix_timestamp (MySQL) 的数据类型是什么?

[英]What is the data type for unix_timestamp (MySQL)?

我应该使用什么数据类型来保存unix_timestamp值 (MySQL)?

the type is integer like :类型是整数,如:

int(11) 

is good for indexing and conditions like > < =适用于索引和条件,例如> < =

You want to use the TIMESTAMP data type .您想使用TIMESTAMP 数据类型
It's stored as an epoch value, but MySQL displays the value as 'YYYY-MM-DD HH:MM:SS'.它存储为纪元值,但 MySQL 将该值显示为“YYYY-MM-DD HH:MM:SS”。

MySql DateTime data type store the date in format 'YYYY-MM-DD HH:MM:SS' with range from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. MySql DateTime数据类型以“YYYY-MM-DD HH:MM:SS”格式存储日期,范围从“1000-01-01 00:00:00”到“9999-12-31 23:59:59”。

MySql TIMESTAMP data type store the date in format 'YYYY-MM-DD HH:MM:SS' with range from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. MySql TIMESTAMP数据类型以 'YYYY-MM-DD HH:MM:SS' 格式存储日期,范围从 '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07'世界标准时间。

Unix TIMESTAMP is the number of seconds since 1970-01-01 , if you want to store the unix Timestamp in mysql db you should use int(11) with attribute UNSIGNED (to permit only positive numbers), and if you want to save the number of microseconds you should use bigint(20) ,.. Unix TIMESTAMP是自1970-01-01以来的秒数,如果你想在mysql db 中存储 unix Timestamp 你应该使用int(11)和属性UNSIGNED (只允许正数),如果你想保存您应该使用bigint(20)的微秒数,..

If you want to get the unixtimestamp in readable format in your select query You can use如果您想在选择查询中以可读格式获取unixtimestamp您可以使用

SELECT FROM_UNIXTIME(CAST(yourtable.start_time as UNSIGNED)) as date_time

If you are using php you can use:如果您使用的是php ,则可以使用:

$unixtimestamp= time();//1544619186

echo(date("Y-m-d", $unixtimestamp));//2018-12-12

If you want to display the datetime using the local timezone in Javascript use this function如果要在Javascript 中使用本地时区显示日期时间,请使用此函数

  function timeConverter(UNIX_timestamp){
        var date = new Date(UNIX_timestamp*1000);
        var year = date.getFullYear();
        var month = ("0"+(date.getMonth()+1)).substr(-2);
        var day = ("0"+date.getDate()).substr(-2);
        var hour = ("0"+date.getHours()).substr(-2);
        var minutes = ("0"+date.getMinutes()).substr(-2);
        var seconds = ("0"+date.getSeconds()).substr(-2);
    
        return year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+seconds;
    }
   console.log(timeConverter(value));// 2018-14-12 13:11:33

(In this case the server should return the unixTimestamp as it is: SELECT yourtable.start_time as date_time ) (在这种情况下,服务器应按原样返回 unixTimestamp: SELECT yourtable.start_time as date_time

BIGINT UNSIGNED NOT NULL BIGINT 未签名非空

Epoch representation in seconds can fit into INT(11) If the timestamp has milliseconds, INT(11) doesn't work in MySQL.以秒为单位的纪元表示可以适合 INT(11) 如果时间戳有毫秒,则 INT(11) 在 MySQL 中不起作用。

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

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