我如何创建一个列来存储在mysql中创建该行的时间? [英] How can I make a column that stores the time that row was created in mysql?
问题描述
经过大量的谷歌搜索后,我找不到答案.我想在mysql中创建一列,将创建该行的时间存储为HH:MM:SS
After tons of googling I could not find an answer. I want to create a column in mysql that stores the the time that row was created as HH:MM:SS
我正在聊天室中,该表格当前有一列用于显示消息和ID
I'm making a chat room and the table currently has a column for the message and id
我不想使用时间戳或日期时间,因为我不想有日期,月份,年等.只有小时分和秒.
I dont want do use timestamp or datetime as I don't want to have date month year etc. just Hour min and sec.
我尝试过时间,但总是说时间是00:00:00
I tried time but it always say the time is 00:00:00
编辑:我想我只希望能够从时间戳记中回显时间.
I guess I want to be able to echo only time from time stamp.
我尝试了echo '<div><p>User@' . substr($value['Time'], 11) . '->' . $value['message'] . '</p></div>';
,但是它不起作用.
I tried echo '<div><p>User@' . substr($value['Time'], 11) . '->' . $value['message'] . '</p></div>';
But it does not work.
推荐答案
编辑答案
如果要在MySQL中使用常规的TIMESTAMP
字段,请首先创建该字段.
Edited Answer
If you want to use a regular TIMESTAMP
field in MySQL, first create the field.
ALTER TABLE `YourTable` ADD `LastAccessed` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
一旦您设置了该字段,就可以不用管它了.当您插入一个记录传递null
作为值时,它将默认为当前时间.当您更新记录时,时间将被调整.
Once you have setup the field you can leave it alone. When you insert a record pass null
as the value and it will default to the current time. When you update the record the time will be adjusted.
要从TIMESTAMP
字段获取时间,您将需要运行选择查询并获取结果.出于说明目的,这看起来类似于以下内容:
To obtain the time from the TIMESTAMP
field you will need to run your select query and get your results. For illustrative purposes, this would look something like the following:
//Integrate this into your existing code
$result = mysqli_query($connection, "SELECT LastAccessed FROM YourTable"); //get only the time field for this example
$row = mysqli_fetch_array($result);
$time = new DateTime($row['LastAccessed']); //The constructor accepts the time information as an argument
echo $time->format("H:i:s"); //This is your time information.
由于现在有了一个TIMESTAMP
字段,数据库不仅将负责以正确的时间更新它,而且您始终可以修改$time->format()
方法的参数以生成带有各种时间数据的字符串.不同的方式.
Since you have a TIMESTAMP
field now, not only will the database take responsibility for updating it with the correct time, but you can always modify the $time->format()
method's argument to produce a string with time data in a variety of different ways.
时间戳被抽象化了,您的应用程序也获得了灵活性.
The timestamp gets abstracted and your application gain flexibility.
双赢,对吧?
在MySQL中创建一个char(8)
列,然后将调用的输出发送给它:
Create a char(8)
column in MySQL and then send it the output from a call to:
date("H:i:s");
尽管在MySQL中使用时间戳字段是最佳选择,但考虑到与记录进行交互时可以对其进行更新.您没有使用整个时间戳.
Although using a timestamp field in MySQL would be optimal, considering you can have it update when the record is interacted with. You don't have to use the entire timestamp.
- Date function in PHP
- Working with timestamps in MySQL
这篇关于我如何创建一个列来存储在mysql中创建该行的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!