首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要我的系统获取系统当前时间

问我需要我的系统获取系统当前时间
EN

Stack Overflow用户
提问于 2013-12-17 21:04:35
回答 2查看 55关注 0票数 2

我的系统是用PHP和MySQL开发的,有两个HTML域,分别名为Time_in和Time_out。

我希望这两个字段都获取系统当前时间。

你会推荐什么代码?

下面是插入一些字段和Time_in , Time_out字段的代码行。

代码语言:javascript
复制
$sql = "INSERT INTO $tbl_name VALUES ('$BID', '$Route', '$Driver_Name',
                 '$Driver_phone_no', '$Time_in', '$Time_out', '$Date' , '$Comment')";
EN

回答 2

Stack Overflow用户

发布于 2013-12-17 21:05:44

使用获取当前日期和时间。

票数 5
EN

Stack Overflow用户

发布于 2013-12-17 21:14:40

您可以使用NOW()来设置当前的时间戳,或者设置一个insert trigger (可能还有update触发器),这样您就不需要记住显式地设置这两个字段的值。

但是,如果需要在设置了这样的触发器的表中“处理”数据,并且不希望将所有行都更新为新的time/timeout值,则要小心。

下面是一个创建表和触发器的示例。然后将一行插入到该表中,并在不久之后进行更新。请注意,即使更新只显式更新注释,time_out值也会更改。

代码语言:javascript
复制
mysql> create table tableName (`id` int(10) unsigned not null auto_increment, `route` varchar(50), `driver_name` varchar(50), `comment` blob default '', `time_in` timestamp , `time_out` timestamp , primary key(`id`)) engine=MyISAM DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (0.07 sec)

mysql> create trigger tableName_bInsert BEFORE INSERT on tableName for each row set new.time_in=NOW();
Query OK, 0 rows affected (0.11 sec)

mysql> create trigger tableName_bUpdate BEFORE UPDATE on tableName for each row set new.time_out=NOW();
Query OK, 0 rows affected (0.09 sec)

mysql> insert into tableName set route="Belfast-Tralee", driver_name="Ken", comment="Pick up dogs. In.";
Query OK, 1 row affected (0.00 sec)

mysql> select * from tableName;
+----+----------------+-------------+-------------------+---------------------+---------------------+
| id | route          | driver_name | comment           | time_in             | time_out            |
+----+----------------+-------------+-------------------+---------------------+---------------------+
|  1 | Belfast-Tralee | Ken         | Pick up dogs. In. | 2013-12-17 13:51:13 | 0000-00-00 00:00:00 |
+----+----------------+-------------+-------------------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> update tableName set comment="Dogs - out" where id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tableName;
+----+----------------+-------------+------------+---------------------+---------------------+
| id | route          | driver_name | comment    | time_in             | time_out            |
+----+----------------+-------------+------------+---------------------+---------------------+
|  1 | Belfast-Tralee | Ken         | Dogs - out | 2013-12-17 13:51:37 | 2013-12-17 13:51:37 |
+----+----------------+-------------+------------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20634967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档