get range between two timestamp - mysql

I like to get the total hours in between two timestamp.
Take a look at my code
$sql="UPDATE timekeeping SET end= timestamp(NOW()),totalrange = ((end- begin) - 1)
WHERE end IS NULL and fullname = '$whoareyou[fullname]'";
but in this code "totalhours = ((end- begin) - 1)" is wrong i feel it :)
I want this to show something like this
2014-05-07 02:00:38.000000 - 2014-05-07 06:00:38.000000 = 4
and something like this
2014-05-07 02:00:38.000000 - 2014-05-07 06:30:38.000000 = 4.30
but i do not know where or what to do.
Please help anyone. I have done my part in researching and found no suitable answer which I can understand since this was my first time to use php.

The following SQL query should do the trick:
SELECT TIMESTAMPDIFF(HOUR, '2012-06-06 13:13:55', '2012-06-06 15:20:18');
Replace the second argument with the date they last logged in and the third argument with the date of their current login.
This will return an integer equal to the hours between the first time stamp and the second time stamp.
EDIT: If you wish to get also the minutes, then replace 'HOUR' with 'MINUTE' and do some math to get the decimal value of Hours.Minutes.

Related

How to add extra 5 minutes in mysql using query

I'm new for mysql, Already value in time field, I want to update extra 5 minutes in time field using query. I tried so many things but not working.
Here my query:
UPDATE STUDENT SET START_TIME = ADDTIME(START_TIME, 500) WHERE ID = 1;
Above query working but one issue is there that is, If my field having 23:55:00.
I want result after executing query 00:00:00 but it updates 24:00:00.
Anyone help me!
Thanks in advance!!
This is bit tricky, because you only have the time, and you want it to wrap around to 0 after hitting 24 hours. My approach is to extract the number of seconds from START_DATE, add 5 minutes, then take the mod of this by 24 hours to wrap around to zero if it exceeds one day's worth of seconds.
UPDATE STUDENT
SET START_TIME = CAST(STR_TO_DATE(CAST(MOD((TIME_TO_SEC(START_TIME) + 300), 86400) AS CHAR(5)), '%s') AS TIME)
WHERE ID = 1
In the demo below, you can see the logic in action which correctly converts 23:55:00 with five minutes added to become 00:00:00.
SQLFiddle
However, the easiest solution in your case might be to just use a DATETIME and ignore the date component. Then the time should wrap automatically to a new day.
select addtime('23:55:00', '00:06:00');
output - 24:01:00 (Ideally it is right, because time datatype represents only time, if it converts to 00:01:00 then time component looses 24hr, which is wrong)
select addtime('2016-09-01 23:55:00', '00:06:00');
output - 2016-09-02 00:01:00 (In this case, 24hr gets added in date so time component is represented as 00:01:00)
If the requirement is to get it as 00:01:00 then here is the workaround -
SELECT TIME((ADDTIME(TIME('23:59:59'), TIME('02:00:00')))%(TIME('24:00:00')));
reference -
ADDTIME() return 24 hour time

Adding Time to MYSQL DATETIME fields?

I have a system where tablets perform tasks and report back with what tasks have been completed and when. One of the tablets has come out of sync and the time is 2 days, 3 hours, 31 mins and 31 secs behind, which is messing up my reporting.
Is there an easy way to add the missing time to the fields with a query?
I can identify which fields need to be altered as they are all tied to a single job, so can add WHERE job_id = 998 to the end.
I have had a look around, and can't really make sense of answers provided, and can't afford to mess up and insert the incorrect times.
Just for clarification, I basically have an event that the tablet believes happened at '2015-12-01 07:57:30' but actually happened at '2015-12-03 11:29:01'. I worked that one out manually, but I have over 100 rows that need updating. Something like: UPDATE job_logs SET entry_time = <CURRENT FIELD DATE> ADD <OUT OF SYNC TIME> WHERE job_id = 998;
Any answers are appreciated, additional detail would be preferred as I'm still learning & would like to understand how the solution works...
Thanks in advance!
You can use MySQL DATE_ADD() function, check this below given SELECT query with your <CURRENT FIELD DATE> field
SELECT <CURRENT FIELD DATE>,DATE_ADD(<CURRENT FIELD DATE>, INTERVAL '2 3:31:31' DAY_SECOND)
FROM job_logs
WHERE job_id = 998;
then you can use same in your UPDATE query as
UPDATE job_logs
SET entry_time = DATE_ADD(<CURRENT FIELD DATE>, INTERVAL '2 3:31:31' DAY_SECOND)
WHERE job_id = 998;
I hope this works...
there is no issue with adding date or time in a datetime filed in mysql
UPDATE job_logs SET entry_time = ADDTIME(entry_time , '1:2:3') WHERE job_id = 998
this will update your entry_time to 1 hour 2 min and 3 sec to existing entry_time.

Calculating time difference before 6am and after 10pm in MySQL

I have one question regarding MySQL date / time functions. For now, I have query which looks like this:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(sum)))
FROM workingtime
WHERE user='magdalena'
AND type='work'
AND start BETWEEN '2014-03-01' AND '2014-03-12'
AND completed=1
So in my database there are 2 columns with timestamps, first one is "start", second one is "end". What I would like to execute in sigle query is this: I would like to have returned how much is the time difference between start and 6am + how much is the time difference between 10pm and end (with option for nex day...). I need this for night shift hours - so I need sum of nightshift hours together as a result.
Example:
start = 2014-02-26 03:30:00
end = 2014-02-26 12:16:59
I would like to get difference between start and 6am and 10pm and end.
In this case: difference between 3:30:00 and 6:00:00 is 2:30:00. Difference between 10pm and end is nothing in this case, because end time is not over 10pm at all. So the result in this case will be 2:50:00. That is the output I would like to get.
Is this possible only with MySQL?
Thank you guys, I appreciate it.
Use can use the TIMEDIFF function, like this:
select
CONCAT(HOUR(TIMEDIFF(starttime, CONCAT(DATE(starttime),' 06:00:00'))), ':', MINUTE(TIMEDIFF(starttime, CONCAT(DATE(starttime),' 06:00:00')))) AS startdiff,
CONCAT(HOUR(TIMEDIFF(CONCAT(DATE(endtime),' 22:00:00'), endtime)), ':', MINUTE(TIMEDIFF(CONCAT(DATE(endtime),' 22:00:00'), endtime))) AS enddiff
from workingtime
Working demo: http://sqlfiddle.com/#!2/fc621/1
I was not able to understand the following part of your question: "with option for next day"

MySQL time between returning false and it should be true

SELECT *
FROM tablename
WHERE
MAKETIME(3,0,0) BETWEEN MAKETIME(23,0,0) AND MAKETIME(5,0,0)
is returning nothing And 3:00 is between 23:00 AND 5:00 time. Why is that can anyone explain me how to solve this problem?
It's unclear what you're actually trying to do here, because even if 3 were between 5 and 23 your query would simply return every record in the table.
SELECT MAKETIME(3,0,0) BETWEEN MAKETIME(5,0,0) AND MAKETIME(23,0,0)
Returns 0, because 3 is not between 5 and 23.
SELECT MAKETIME(5,0,0) BETWEEN MAKETIME(3,0,0) AND MAKETIME(23,0,0)
Returns 1, because 5 is between 3 and 23.
Demo: SQL Fiddle
Presumably you're trying to wrap into the previous day, in which case you can directly compare datetime values, but it's unclear given your question what fields/datatypes you're actually working with.
Update:
Based on your comment, I think you want 2 comparisons. 3 is not between 5 and 23, because time doesn't wrap across days. But if you only care about the time portion you can handle it like this:
SELECT *
FROM tablename
WHERE YourTime BETWEEN MAKETIME(23,0,0) AND MAKETIME(23,59,59)
OR YourTime BETWEEN MAKETIME(0,0,0) AND MAKETIME(5,0,0)
Remember that BETWEEN is inclusive, so if 5am is your cutoff time you may want it to be MAKETIME(4,59,59) so it includes 4:59 but not 5:00
Function MAKETIME returns a time value calculated from the hour, minute, and second arguments:
mysql> SELECT MAKETIME(3,0,0),MAKETIME(23,0,0),MAKETIME(5,0,0)
-> '03:00:00', '23:00:00', '05:00:00'
and, of course, 3 is not BETWEEN 23 AND 5 and it will return false. But yes, 3AM actually is between 11PM and 5AM, so how could you solve this?
Let's consider 23 as your START_TIME, and 5 as your END_TIME.
Since START_TIME has to happen before END_TIME, if this is not the case (23>5) that means that the interval rolls over the next day.
I would try with a query like this:
SELECT *
FROM yourtable
WHERE
(MAKETIME(START_TIME,0,0)<=MAKETIME(END_TIME,0,0) AND MAKETIME(3,0,0) BETWEEN MAKETIME(START_TIME,0,0) AND MAKETIME(END_TIME,0,0))
OR
(MAKETIME(START_TIME,0,0)>MAKETIME(END_TIME,0,0) AND NOT (MAKETIME(3,0,0) BETWEEN MAKETIME(START_TIME,0,0) AND MAKETIME(END_TIME,0,0)))

How to update timestamp column moving date forward

what I am looking for is some help with a query.
I have a MySql field with unixtime in it representing a date in each of the next few dozen months. I have to move the dates forward to the first day of the next month for each entry in the table.
The dates are all the 20th of each month, and so I want to move June 20 to July 1, July 20 to August 1, and so on. I can't just add 11 days, because that wouldn't be the first day of the next month when considering months with 31 days and February.
I have been playing with ideas like this:
update table set column = UNIX_TIMESTAMP(column + MONTH(column)+1,DAY(1)) where index_column = '1234'
but I am pretty sure that won't work. I could use something like this to convert it, then try to convert it back:
update table set column = DATEFORMAT(column,'%Y-$c-%d %H:%i:%s') where index_column == '1234'
I still think there has to be a better way. Frankly, I would update the few dozen manually, but I know this will come up frequently, and don't want to have to do it manually every time.
I prefer not to use code, but would instead like to just do it directly into MySql. I hope there is someone out there that can help me figure this out.
Thank you in advance.
Maybe something like this Works:
update table set column = UNIX_TIMESTAMP(LAST_DAY(FROM_UNIXTIME(column)) + INTERVAL 1 DAY) where index_column = '1234'
Reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day
Does ADDDATE do it for you? Or do you need something more?