简体   繁体   English

SQL-自日期起的时间

[英]SQL - Time since date

I am struggling with one thing. 我为一件事而苦苦挣扎。 I'm trying to calculate how many days ago a certain date was, by using SQL. 我试图通过使用SQL计算某个日期是多少天前。 The dates I have in my database can be in two formats: 我在数据库中拥有的日期可以采用两种格式:

Aug 28 2014, 17:17:34 CEST
Dec 29 2015, 01:03:14 CET

Those are two examples of different dates. 那是两个不同日期的例子。 Notice the "CET" and "CEST". 注意“ CET”和“ CEST”。

But anyways, how would I go ahead and calculate this in a SQL query? 但是无论如何,我将如何在SQL查询中进行计算呢? I managed to do this in PHP but I'd like to do this in the SQL query itself (if possible). 我设法在PHP中做到这一点,但我想在SQL查询本身中做到这一点(如果可能的话)。 Because it would save up on a lot of memory usage. 因为这样可以节省大量的内存使用量。 I try make my work as fast as possible. 我尝试使我的工作尽可能快。 I want to only access data from users that has only logged in the past 2-3 days or so. 我只想访问过去2-3天左右才登录的用户的数据。 Of course I could make a SELECT * FROM users and then run PHP to check for the dates. 当然,我可以对SELECT * FROM users进行SELECT * FROM users ,然后运行PHP来检查日期。 But is there perhaps a way to do this in SQL? 但是,也许有一种方法可以在SQL中做到这一点? Like: SELECT * FROM users WHERE [lastlogin < 2 days] 赞: SELECT * FROM users WHERE [lastlogin < 2 days]

Here is my current PHP code. 这是我当前的PHP代码。 I'd really want to do this in SQL. 我真的很想用SQL做到这一点。 By the way, my columns are currently in text. 顺便说一句,我的专栏目前是文字。 Datetime does not work with that format for some reason. 由于某种原因,日期时间无法使用该格式。

$lastlogin = $row['lastlogin'];
$lastlogin = str_replace("\xc2\xa0",' ',$lastlogin);

$Date = $lastlogin;
$Date = substr($Date, 0, strpos($Date, " CE"));
$now  = date('Y-m-d');
$datetime1 = new DateTime($Date);
$datetime2 = new DateTime($now);
$interval = $datetime1->diff($datetime2);
$difference = $interval->format('%a days ago');

echo "Last login was: " . $difference;

you should alter your table to clean up the data. 您应该更改表以清理数据。 convert the data to two columns with the timezone info in one column and the date and time in another column. 将数据转换为两列,其中一列为时区信息,另一列为日期和时间。 you can split the data easily using SUBSTRING_INDEX() and convert the string to datetime at the same time. 您可以使用SUBSTRING_INDEX()轻松拆分数据,并将字符串同时转换为日期时间。

split on the "C" of "CET" and "CEST" like this: 像这样在“ CET”和“ CEST”的“ C”上进行拆分:

SELECT SUBSTRING_INDEX("Aug 28 2014, 17:17:34 CEST","C",1)

and you will see you are left with the date and time part only, albeit still in a string format. 并且您会看到只剩下日期和时间部分,尽管仍然是字符串格式。 That can be changed with STR_TO_DATE and you can do both on the fly. 可以使用STR_TO_DATE进行更改,并且您可以同时进行两种操作。

First add the new columns to store the data: 首先添加新列以存储数据:

ALTER yourtablename ADD newdatecolumn DATETIME AFTER oldcolumnname;
ALTER yourtablename ADD newtimezonecolumn VARCHAR(4) AFTER newdatecolumn;

UPDATE yourtablename
SET newdatecolumn = 
STR_TO_DATE(SUBSTRING_INDEX(olddatecolumn,"C",1), '%b %d %Y, %T')

you can then use SUBSTRING_INEX again, this time splitting on the last space in the column and grabbing the timezone for the other new column 然后,您可以再次使用SUBSTRING_INEX,这次在列的最后一个空格上进行划分,并为另一个新列获取时区

UPDATE yourtablename
SET newtimezonecolumn = SUBSTRING_INDEX(olddatecolumn," ",-1)

then you will have data that you can work with more easily to use the suggested DATEDIFF() or other time and date functions. 那么您将拥有可以轻松使用建议DATEDIFF()或其他时间和日期函数的数据。 You can drop your old date column if you need to. 如果需要,可以删除旧的日期列。

Note that yourtablename etc should be changed for actual table and column names. 请注意,应将yourtablename等更改为实际的表名和列名。

You can use to_days() or datediff() functions 您可以使用to_days()或datediff()函数
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

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

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