buladex Posted May 2, 2010 Share Posted May 2, 2010 i need time restriction of 12 hours for voting system, my code looks like this but doesnt respect 12h interval, some time after time pass 00:00 ppl can vote endless until next day at 12:00: $sql="SELECT * FROM $tbl_name WHERE DATE_ADD(CURRENT_DATE(), INTERVAL 12 HOUR) > `last_voted` and name='$myusername'"; any suggestion???? i also try CURRENT_TIME() and didn not work Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Doesn't make sense because for one, CURRENT_DATE() should be already be greater than last_voted, so adding 12 hours to it doesn't do anything. I think you want to subtract or add 12 hours to last_voted. But your logic is off. Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1051854 Share on other sites More sharing options...
JAY6390 Posted May 2, 2010 Share Posted May 2, 2010 $time = time() - 43200; $sql = "SELECT * FROM `$tbl_name` WHERE `last_voted` > '$time' AND `name` = '$myusername'"; That will return rows of any vote within the last 12 hours (43200 seconds) Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1051857 Share on other sites More sharing options...
yperevoznikov Posted May 2, 2010 Share Posted May 2, 2010 @buladex Hi. Can you try this (Usage sql is more faster): $sql="SELECT * FROM $tbl_name WHERE DATE_ADD(NOW(), INTERVAL -12 HOUR) > `last_voted` and name='$myusername'"; Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1051861 Share on other sites More sharing options...
buladex Posted May 2, 2010 Author Share Posted May 2, 2010 im using sql , and my last_voted is a like this `last_voted` = NOW() table save, i tried them all above, atm i have it set like this: $sql="SELECT * FROM $tbl_name WHERE DATE_ADD(CURRENT_DATE(), INTERVAL -12 HOUR) > `last_voted` and name='$myusername'"; works at this time but i need to see later if works cuz i cant change my computer time. ty all for helping ill let u know later if its working Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052072 Share on other sites More sharing options...
buladex Posted May 3, 2010 Author Share Posted May 3, 2010 not good ppl that voted 24 hours are not allowed to vote ( Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052219 Share on other sites More sharing options...
buladex Posted May 3, 2010 Author Share Posted May 3, 2010 Ken2k7 does this make any logic now??? : $sql="SELECT * FROM $tbl_name WHERE DATE_ADD(`last_voted`, INTERVAL 12 HOUR) < CURRENT_DATE() and name='$myusername'"; Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052221 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 Yes, that should work. It's the same concept yperevoznikov posted. It's basic math really. Either you add 12 hours to last_voted or you subtract 12 hours from the current date. Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052293 Share on other sites More sharing options...
buladex Posted May 3, 2010 Author Share Posted May 3, 2010 after it passed 12:00 didnt work ( Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052302 Share on other sites More sharing options...
buladex Posted May 3, 2010 Author Share Posted May 3, 2010 maybe its sql save ... its set on datetime, ill try to make it time but what should i put on sql save 'last_voted"=???? Now()? Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052306 Share on other sites More sharing options...
yperevoznikov Posted May 3, 2010 Share Posted May 3, 2010 Hi Buladex I one of decisions you may try to convert time to timestamp and then compare time (by using UNIX_TIMESTAMP() function). Or you may try better solution - TIMESTAMPDIFF() function. Something like this: TIMESTAMPDIFF(HOUR, `your_time`, NOW()) < 12 or ... > 12 depending on what do you want check. Let me know it this is help you if not - post SQL script you used and examples from DB. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1052599 Share on other sites More sharing options...
buladex Posted May 4, 2010 Author Share Posted May 4, 2010 atm i droped on using time and i do it manualy at 12 h but i need it to make automatic so how does it work to add 12 h in sql and decrease automatic to 0, using time stamp? i mean if u vot u add in sql table +43200 seconnds(12h) what attribute should have the sql table time?timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1053289 Share on other sites More sharing options...
yperevoznikov Posted May 5, 2010 Share Posted May 5, 2010 Hi buladex, That's very strange As I understand your condition is not working only if pass 12 hours sharp. Did you try to use `<=` `>=` instead `<` `>`? About your question: You may convert time to timestamp by UNIX_TIMESTAMP() as I said before or set timestamp as type of your field. Quote Link to comment https://forums.phpfreaks.com/topic/200437-time-interval/#findComment-1053411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.