分割日期和时间,然后将它们转移到各自的新列中

时间:2019-05-27 13:51:58

标签: php sql

我有一个网站,该网站的日期和时间在会议开始和会议结束时相结合。

会议开始 2019-04-26 11:39:15

我已经为日期和时间创建了新列。如何拆分现有的日期和时间,然后将其转移到各自的新列中?

Ps。我正在寻找SQL代码。

3 个答案:

答案 0 :(得分:1)

您可以简单地使用mySQL中可用的日期和时间函数

set @d='2019-04-26 11:39:15';
select date(@d),time(@d);

将输出

+------------+----------+
| date(@d)   | time(@d) |
+------------+----------+
| 2019-04-26 | 11:39:15 |
+------------+----------+

假设现有列名为meeting_start和一个表名为TABLE,那么您可以这样做

update `TABLE` set `date_col`=date( `meeting_start` ), `time_col`=time( `meeting_start` );

一个简单的表模式,用于说明如何更新新字段

mysql> describe `so_datetime_fudge`;
+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name               | varchar(50)      | YES  |     | NULL    |                |
| meeting_start      | datetime         | YES  |     | NULL    |                |
| meeting_start_date | date             | YES  |     | NULL    |                |
| meeting_start_time | time             | YES  |     | NULL    |                |
+--------------------+------------------+------+-----+---------+----------------+

具有三行伪数据的表的快照

mysql> select * from so_datetime_fudge;
+----+-----------------------+---------------------+--------------------+--------------------+
| id | name                  | meeting_start       | meeting_start_date | meeting_start_time |
+----+-----------------------+---------------------+--------------------+--------------------+
|  1 | The First of Too Many | 2019-05-27 16:48:30 | NULL               | NULL               |
|  2 | Holy Cow Batman       | 2019-05-27 16:54:56 | NULL               | NULL               |
|  3 | The End is Nigh       | 2019-05-27 16:55:22 | NULL               | NULL               |
+----+-----------------------+---------------------+--------------------+--------------------+

然后运行此查询:

update `so_datetime_fudge` set `meeting_start_date`=date( `meeting_start` ), `meeting_start_time`=time( `meeting_start` );

在更新后运行相同的SELECT查询

mysql> select * from so_datetime_fudge;
+----+-----------------------+---------------------+--------------------+--------------------+
| id | name                  | meeting_start       | meeting_start_date | meeting_start_time |
+----+-----------------------+---------------------+--------------------+--------------------+
|  1 | The First of Too Many | 2019-05-27 16:48:30 | 2019-05-27         | 16:48:30           |
|  2 | Holy Cow Batman       | 2019-05-27 16:54:56 | 2019-05-27         | 16:54:56           |
|  3 | The End is Nigh       | 2019-05-27 16:55:22 | 2019-05-27         | 16:55:22           |
+----+-----------------------+---------------------+--------------------+--------------------+

但是,如果您创建表的VIEW,则可以按照自己喜欢的方式进行构造,并且基础数据保持不变

CREATE VIEW `vw_datetime_fudge` AS select 
    `id`, 
    `name`, 
    date( `meeting_start` ) as `meeting_start_date`, 
    time( `meeting_start` ) as `meeting_start_time` 
from `so_datetime_fudge`;


mysql> select * from `vw_datetime_fudge`;
+----+-----------------------+--------------------+--------------------+
| id | name                  | meeting_start_date | meeting_start_time |
+----+-----------------------+--------------------+--------------------+
|  1 | The First of Too Many | 2019-05-27         | 16:48:30           |
|  2 | Holy Cow Batman       | 2019-05-27         | 16:54:56           |
|  3 | The End is Nigh       | 2019-05-27         | 16:55:22           |
+----+-----------------------+--------------------+--------------------+

答案 1 :(得分:0)

<?php

$subject = 'Meeting_start 2019-04-26 11:39:15';

$pattern = '/(\d\d\d\d\-\d\d\-\d\d).*(\d\d\:\d\d\:\d\d)/U';


preg_match_all($pattern,$subject,$matches);

$date = $matches[1][0];
$time = $matches[2][0];

echo $date . '<br>' . PHP_EOL;
echo $time . '<br>' . PHP_EOL;

给出结果

2019-04-26<br>
11:39:15<br>

答案 2 :(得分:0)

您可以使用DateTime::format方法

$date = new DateTime('2019-04-26 11:39:15');
$timeString = $date->format('H:i:s');
$dateString = $date->format('Y-m-d');