Jump to content

php variables to compare to mysql sections in mysql datetime field


markymark87

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.