froggie81 Posted September 2, 2009 Share Posted September 2, 2009 Hi ; New to this forum and home a question like this is ok. I cant quote get this right . Could some one supply a little chunk of code to get me started? In mysql db I have 4 fields start_date:type date - start_time:type time - end_date:type date - end_time:type time. In phpmyadmin browse function they look like 2009-08-01 and 15:00:00. These fields work perfectly for the UI I have written with drop down select lists. OK , In psuodo I need to code is : if now() is >= (start date/time) and Now() <= (end date/time) bla bla bla. I am doing this check to see that someone is loggin in bewtween the allowed window. Could anyone give me a code example? Thank You! Frank Quote Link to comment https://forums.phpfreaks.com/topic/172825-little-help-with-datetime-cpmparison/ Share on other sites More sharing options...
Pickle Posted September 2, 2009 Share Posted September 2, 2009 i think you need to make the now() element to be in the same format as the date you care comparing it with or the other way round. use date functions to get them into the same format then compare them. hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/172825-little-help-with-datetime-cpmparison/#findComment-910881 Share on other sites More sharing options...
ignace Posted September 2, 2009 Share Posted September 2, 2009 You could create one field of start_date and start_time of type DATETIME and the same for end_date, end_time afterwards you can perform a query like: SELECT * FROM table WHERE username = $username AND password = $password AND now() BETWEEN start AND end I'm currently looking for a function that can combine a date and time field as a datetime field. Edit: This should work using the start_date, start_time, end_date, end_time: SELECT * FROM table WHERE username = $username AND password = $password AND now() BETWEEN date_add(start_date, interval start_time HOUR_SECOND) AND date_add(end_date, interval end_time HOUR_SECOND) Quote Link to comment https://forums.phpfreaks.com/topic/172825-little-help-with-datetime-cpmparison/#findComment-910883 Share on other sites More sharing options...
froggie81 Posted September 3, 2009 Author Share Posted September 3, 2009 Thank you all for your input. If it would be of value to anyone else,(I always appreciate when people post solutions and not disappear after getting assistance) This works beautifully: // EXTRACT DATA FROM DB $start_date=$row['start_date']; $start_time=$row['start_time']; $end_date=$row['end_date']; $end_time=$row['end_time']; // CREATE UNIX TIMESTAMPS $startdatetime=strtotime("$start_date $start_time"); $enddatetime=strtotime("$end_date $end_time"); $accesstime = time(); /* NOW */ if (($accesstime >= $startdatetime) && ($accesstime <= $enddatetime)) Thanks, Frank Quote Link to comment https://forums.phpfreaks.com/topic/172825-little-help-with-datetime-cpmparison/#findComment-911785 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.