I want to ask how to find the time difference of two fields in minutes. eg in 'in' field filled 2014-07-12 19:00:00 , and in 'out' field filled 2014-07-12 20:30:00 , the time difference will be automatically saved in the 'duration' field
----in--------------|-------out-----------| duration
2014-07-12 19:00:00 | 0000-00-00 00:00:00 | 0
--------------------------------------------------
when i click update button it will updated like,
-------in-----------|---------out---------| duration
2014-07-12 19:00:00 | 2014-07-12 20:30:00 | 90
--------------------------------------------------
thank you
You can use the TIMESTAMPDIFF function, as below:
SELECT
TIMESTAMPDIFF(MINUTE, in, out) duration
FROM mytable;
Reference:
TIMESTAMPDIFF function on MySQL Reference Manual
Manual for TIME_TO_SEC function.
SELECT (TIME_TO_SEC(`in`)-TIME_TO_SEC(`out`))/60 AS duration FROM TABLE_NAME ;
Related
I have a mysql "users" table like this example:
id | user | created
100 | user001 | [27-01-2016 04:30 PM]
101 | user005 | [19-05-2017 09:28 AM]
102 | user019 | [09-10-2015 03:29 PM]
103 | user029 | [18-11-2017 05:40 PM]
And I want to get this table in descending order by created row like this:
id | user | created
103 | user029 | [18-11-2017 05:40 PM]
101 | user005 | [19-05-2017 09:28 AM]
100 | user001 | [27-01-2016 04:30 PM]
102 | user019 | [09-10-2015 03:29 PM]
I tried with DATE_FORMAT:
SELECT id,user,DATE_FORMAT(created,"%d %m %Y") AS created FROM users ORDER by created desc
But it shows nothing, how can I get it?
I'd like some help.
This works:
SELECT `id`, `user`, DATE_FORMAT(STR_TO_DATE(MID(`created`, 2, 19), '%d-%m-%Y %h:%i %p'), '%d-%m-%Y %h:%i %p')
AS `created:` FROM `users` ORDER BY STR_TO_DATE(`created:`,'%d-%m-%Y %h:%i %p') DESC
Here is a working SQL Fiddle.
Explanation:
The MID() MySQL function returns the middle part of a string, so we easily stripped out the []. After that, the STR_TO_DATE() function parses the returned string into DATETIME format (the %p is for AM/PM). The returning value is not properly formatted, at least not in the way you would wish to have it. So we then use DATE_FORMAT() to format the output. In the end, we use STR_TO_DATE() once again for the ordering since created: is returned as string.
I also changed your created output column to created: since it will not work if they are all the same name.
Your created date format isn't correct, you should alter the table and change the field from varchar/char(string) to timestamp type. I have already tried converting your string dates with 'STR_TO_DATE()' but it returns null because the format doesn't match either timestamp, date or time formats. Its always best practice to store dates/datetime as either date or timestamp data types. You can always apply formatting functions later in query to output dates to any desired format.
This is the table:
+----+-----------+------------+----------+
| id | id_sensor | start_time | end_time |
+----+-----------+------------+----------+
| 1 | 12 | 21:15:00 | 02:45:00 |
| 2 | 7 | 00:00:00 | 23:15:00 |
| 3 | 5 | 04:30:00 | 16:30:00 |
+----+-----------+------------+----------+
I need to get record(s) where a specific time (e.g. 01:00:00) passed by PHP is between. start_time and end_time are TIME fields in UTC, I'm passing to the query hour via php, note, converted in php from user_timezone to UTC.
SELECT * FROM test WHERE TIME('01:00:00') BETWEEN start_time AND end_time;
Query returns only record id 2, not the 1. I need both, in this case (for id 1, end time ofcourse is next day).
Of course, if we looking for TIME('01:00:00'), we don't need the id 3.
Thank you.
I think this is the logic you want:
SELECT *
FROM test
WHERE (start_time < end_time AND TIME('01:00:00') BETWEEN start_time AND end_time) OR
(start_time > end_time AND TIME('01:00:00') NOT BETWEEN end_time AND start_time);
now() returns both, current time and date. You can also work with curtime(), which returns only the current time.
BTW, i think that working with SELECT * should be avoided (maybe you used it just for this example), it is IMHO always better to list the fields needed explicitly.
I need to store a deadline, that consists of a date and a time (e.g 2016-05-02 19:02). I am currently using a field that has DATETIME as datatype but the problem is that its automatically saving it as 2016-05-02 19:02:00.
as a solution i was thinking to save the date in a String field.
So i am wondering if i should do that ? any performance advantages/disadvantages ?
Don't fight the database. Use the builtin types unless you really need something that they can't offer(I'd say it's unlikely, though). (And by this I mean that you should use TIME, DATE or similar for times and dates. Then you can do calculations without having to convert values, etc)
If you don't need the seconds then just keep them 00 all the time.
Whether you use DATETIME, DATE & TIME or perhaps TIMESTAMP is up to you, how you use the data. Choose the alternative that makes most sense in your current situation.
As mentioned in the other answer, you should always use built-in data types whenever possible.
In your case, stick with DATETIME and then convert it to whatever format you need in the query using the DATE_FORMAT function, like so:
mysql> SELECT * FROM `mytable`;
+----+---------------------+
| id | mydatetime |
+----+---------------------+
| 1 | 2016-06-06 14:12:00 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(`mydatetime`,'%b %d %Y %h:%i %p') AS `mydatetime` FROM `mytable`;
+----------------------+
| mydatetime |
+----------------------+
| Jun 06 2016 02:12 PM |
+----------------------+
1 row in set (0.00 sec)
Reference:
http://www.w3schools.com/sql/func_date_format.asp
I am working on a project where I have many operations to manage.
Each operation have an end date and is composed by a certain amount of tasks.
I want to display reminders (a text displayed on the screen) if a task is not done before [end date] - X days.
All the data is stored in MySQL database and I work with PHP and HTML5.
Which datatype is (are) the best to work with date and days (to
perform calculations)?
Can I work with Date() and subtract days in a easy way?
I do not have a specific technical question, but I think sharing best way to proceed is a good thing, right?
I'm curious to know what are the best ways to proceed and open to any proposal!
I recommend to store your date in mysql at field timestamp because you can use default value CURRENT_TIMESTAMP - it very helpful,
and i think you shouldn't worry about it, there is a plenty of functions::
mysql:
select now();
+---------------------+
| now() |
+---------------------+
| 2015-04-08 12:13:18 |
+---------------------+
select now() - interval 1 day;
+------------------------+
| now() - interval 1 day |
+------------------------+
| 2015-04-07 12:13:29 |
+------------------------+
select now() - interval 7 day;
+------------------------+
| now() - interval 7 day |
+------------------------+
| 2015-04-01 12:13:38 |
+------------------------+
select now() - interval 1 month;
+--------------------------+
| now() - interval 1 month |
+--------------------------+
| 2015-03-08 12:13:58 |
+--------------------------+
php:
<?php
var_export([
date('Y-m-d H:i:s', strtotime('now')),
date('Y-m-d H:i:s', strtotime('- 1 day')),
date('Y-m-d H:i:s', strtotime('- 7 day')),
date('Y-m-d H:i:s', strtotime('- 1 month')),
]);
/*
Result:
array (
0 => '2015-04-08 15:15:42',
1 => '2015-04-07 15:15:42',
2 => '2015-04-01 15:15:42',
3 => '2015-03-08 15:15:42',
)
*/
And sometimes very helpful to create table like:
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
in result your field ts will be automatically seted and updated...
You can store dates as DATETIME in your database.
Then in PHP convert it to manageable data using strtotime() and the date() functions
The best data type to work with is the DateTime class.
In order to perform substractions using the DateTime class, you'll need to use the DateInterval class.
You'll need some time to get on your ease using those two classes but it will make formatting or date operations easier afterwoods.
I got a timestamp record in my msql db which has date and time.
I would to get just the time from it, or just time hour and minute.
How can I split that record?
EDIT here is a picture of my table:
Use MySQL's TIME() function:
SELECT TIME(my_column) FROM my_table
To obtain only the date part, you can use the DATE() function.
Also can try this:
SELECT DATE_FORMAT(now(),'%r') AS timestamp;
| TIMESTAMP |
---------------
| 03:51:43 PM |
SELECT DATE_FORMAT(your_Datetimecolumn,'%r') AS timestamp;
You can also use TIME_FORMAT or DATE_FORMAT to get a specific format e.g. only hh:mm
SELECT TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' );
+-------------------------------------------+
| TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' ) |
+-------------------------------------------+
| 17:25 |
+-------------------------------------------+
1 row in set (0.00 sec)
EDIT
The function of course works for columns of a table as well
SELECT TIME_FORMAT( myColumn, '%H:%i' ) FROM myTable;