If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Adding 2 hours to the current time problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-27-04, 09:52
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
Adding 2 hours to the current time problem

Hello,
I am trying to add 2 hours to the current time to compare it with a time in my db, I have try several different ways after looking at the MySQl manual but can't get it to work:

Code:
SELECT ADDTIME(CURTIME(), INTERVAL 2 HOUR);
Thanking you
Reply With Quote
  #2 (permalink)  
Old 10-27-04, 10:32
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

Did you try :

Code:
select ADDTIME(CURTIME(), '02:00:00.000000') 
from DUAL;
or :

Code:
select SEC_TO_TIME(TIME_TO_SEC(CURTIME()) + 7200) 
from DUAL;
Regards,

RBARAER
Reply With Quote
  #3 (permalink)  
Old 10-27-04, 11:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
fyi there is no DUAL table in mysql (unless you create one yourself)

in mysql it is not necessary to select from a table

SELECT now(), ADDDATE(now(), INTERVAL 2 HOUR) as newtime

--> 2004-10-27 08:04:42 2004-10-27 10:04:42
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-27-04, 11:13
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Sorry, Rudy, it may only be a "dummy" feature, but it now exists (OK I'm becoming too used to Oracle ). See the MySQL Manual :

Quote:
From MySQL 4.1.0 on, you are allowed to specify DUAL as a dummy table name in situations where no tables are referenced:

mysql> SELECT 1 + 1 FROM DUAL;
-> 2

DUAL is purely a compatibility feature. Some other servers require this syntax.
Best Regards,

RBARAER
Reply With Quote
  #5 (permalink)  
Old 10-27-04, 11:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
well, whaddya know, ya learn sumpin new every day!!

thanks for the correction

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-27-04, 11:58
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
Thanks guys,
I can now select and add the time but when comparing with times in the DB it is not working as I expected, i am trying to select only records where the time in the database + 1 hour is superior at the current time but all records are returned:

Code:
SELECT codeId, codetime, ADDDATE(codetime, INTERVAL 1 HOUR) as limittime FROM tbllogin_codes WHERE ADDDATE(codetime, INTERVAL 1 HOUR) > curtime();
Here is the result:
+----------------+---------------------+---------------------+
| codeId | codetime | limittime |
+----------------+---------------------+---------------------+
| 10988829451451 | 2004-10-27 16:31:00 | 2004-10-27 17:31:00 |
| fdfd | 2004-10-27 15:31:00 | 2004-10-27 16:31:00 |
| 10988910604261 | 2004-10-27 05:31:00 | 2004-10-27 06:31:00 |
+----------------+---------------------+---------------------+

Any idea where I am going wrong there?
Reply With Quote
  #7 (permalink)  
Old 10-27-04, 13:04
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
ADDDATE() returns a date-time, not a time, so try using CURDATE() instead of CURTIME() :

Code:
SELECT codeId, codetime, ADDDATE(codetime, INTERVAL 1 HOUR) as limittime 
FROM tbllogin_codes 
WHERE ADDDATE(codetime, INTERVAL 1 HOUR) > curdate();
Regards,

RBARAER
Reply With Quote
  #8 (permalink)  
Old 10-29-04, 06:47
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
Hello,
i have changed from time to date but it is still not working as expected, it still returns very records that have the smae date as the current date regardless of the time, should I change the DB and use a time field in stead?
Regards,
Reply With Quote
  #9 (permalink)  
Old 10-29-04, 08:09
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Did you try :
Code:
SELECT codeId, codetime, SEC_TO_TIME(TIME_TO_SEC(codetime) + 7200) as limittime 
FROM tbllogin_codes 
WHERE SEC_TO_TIME(TIME_TO_SEC(codetime) + 7200)  > curtime();
or :
Code:
SELECT codeId, codetime, ADDTIME(codetime, '02:00:00.000000') as limittime 
FROM tbllogin_codes 
WHERE ADDTIME(codetime, '02:00:00.000000') > curtime();
as I already suggested ?

Regards,

RBARAER
Reply With Quote
  #10 (permalink)  
Old 10-29-04, 09:01
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
Hello,
the second suggestion is giving me an error, i don't seem to be able to use ADDTIME()

The second version doesn't give an error but return just about all records in the table, I have added the current time in the query:

+----------------+---------------------+-----------+----------+
| codeId | codetime | limittime | thetime |
+----------------+---------------------+-----------+----------+
| 10988957459411 | 2004-10-27 17:49:05 | 19:49:05 | 13:55:39 |
| 10988968023791 | 2004-10-27 18:06:42 | 20:06:42 | 13:55:39 |
| 10988969316451 | 2004-10-27 18:08:51 | 20:08:51 | 13:55:39 |
| 10988970206761 | 2004-10-27 18:10:20 | 20:10:20 | 13:55:39 |
| 10988970790201 | 2004-10-27 18:11:19 | 20:11:19 | 13:55:39 |
| 10988972871131 | 2004-10-27 18:14:47 | 20:14:47 | 13:55:39 |
| 10988973890201 | 2004-10-27 18:16:29 | 20:16:29 | 13:55:39 |
| 10988975970041 | 2004-10-27 18:19:57 | 20:19:57 | 13:55:39 |
| 10988976488631 | 2004-10-27 18:20:48 | 20:20:48 | 13:55:39 |
| 10990530569531 | 2004-10-29 13:30:56 | 15:30:56 | 13:55:39 |
+----------------+---------------------+-----------+----------+

Thanking you for your help,
olivier
Reply With Quote
  #11 (permalink)  
Old 10-29-04, 09:46
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Ok,

The problem comes from your "codetime" field being a DATE, not a TIME... We are just turning MySQL mad with DATETIME / TIME functions or convertions ! TIME_TO_SEC should only be used with TIME, and ADDTIME(datetime) cannot be correctly compared to a time. And unfortunately, when we tried CURDATE(), it was returning a date without time portion. Use Now(), which returns the current DATE AND TIME :
Code:
SELECT codeId, codetime, ADDTIME(codetime, '02:00:00.000000') as limittime 
FROM tbllogin_codes 
WHERE ADDTIME(codetime, '02:00:00.000000') > Now();
That should work.

Regards,

RBARAER
Reply With Quote
  #12 (permalink)  
Old 10-29-04, 10:03
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
hello,
more bad news, I am getting a error 1064, check the manual.............
It seems that every time I try to use ADDTIME() i get an error, thus I use one of the latest version.
I have try to change the field to a time instead of a date / time but I get the same error, have also try different suggestions from above without any result!

Just can't figure it out!

Any idea what the problem could be?

Thanking you,
Olivier
Reply With Quote
  #13 (permalink)  
Old 10-29-04, 10:20
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
This one does not work even with a TIME field ?

Code:
SELECT codeId, codetime, SEC_TO_TIME(TIME_TO_SEC(codetime) + 7200) as limittime 
FROM tbllogin_codes 
WHERE SEC_TO_TIME(TIME_TO_SEC(codetime) + 7200)  > curtime();
Reply With Quote
  #14 (permalink)  
Old 10-29-04, 10:47
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
No it is returning all the rows
Reply With Quote
  #15 (permalink)  
Old 10-29-04, 11:24
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Are you sure ? Are you sure that, in the rows you posted before, the last one (provided the date part is not here anymore) is still returned ? Other ones should be, but not the last one. Of course, since it is about 17:00, if all your rows are like time + 2 hours = 20:00, they will all be returned.

Please check this.

Regards,

RBARAER
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On