markymark87 Posted October 11, 2007 Share Posted October 11, 2007 Hi, i have a huge problem and 1 that i cannot progress any further with, without any help. Basically this is just a home project. I have got the users to enter a date, month, hr & min to see when the next train is available for them(so basically this is home project of virgintrains or nationalrail or whatever) & in my database i have a datetime field consisting of the times that the trains run. (im doing this with AJAX by the way so if anyone has any good tutorials about that then great). so what i want to do is compare the exact date of eg 2007-10-11. then when i compare the time part then i want to search for the next few hrs. say the user entered 12:45:00 (oh the year 2007 and the seconds of '00' are default, the user doesnt enter them) i want it to look for the next 2 hrs or so. the other way of limiting it is to actually limit in the query. how do i go about first splitting the datetime up and then comparing the hh & mm to seperate variables? how do i go about making it search for trains within the next 2 hrs and arent just the trains that run at 12:45:00 to that exact location? because i am getting the user to enter where they are leaving from and going to aswell? Quote Link to comment https://forums.phpfreaks.com/topic/72766-php-variables-to-compare-to-mysql-sections-in-mysql-datetime-field/ Share on other sites More sharing options...
agazad Posted October 11, 2007 Share Posted October 11, 2007 The user selects their date/time, which is stored in $userdate (as a date/timestamp). In your search result query, I think you need to first of all set up a.. $last = $userdate + 3 $latest = $userdate - 3 might look something like //check syntax! $last = dateadd("h",3,$userdate); # add 3 hours to date $latest = dateadd("h",3,$userdate); # subtract 3 hours to date then in the query, search between the vars $last and $latest for trains LIMIT to how ever many results you like. don't split the date up, use PHPs date function - providing your train date/time is set in the db as date/timestamps you'll be able to use phps date function, which is really usable. Probably not the best solution, I've not been PHPing for long. Might give it a go though. Quote Link to comment https://forums.phpfreaks.com/topic/72766-php-variables-to-compare-to-mysql-sections-in-mysql-datetime-field/#findComment-367019 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.