Jump to content

Querying MYSQL Date using PHP Variable


millsy007

Recommended Posts

I have a website form where the user selects:

 

A date (from a calendar) eg 01/30/2009

A time from a drop down list eg 09:00

 

I then want to combine these values and query my mysql table, but I am unsure of the format to put it in.

 

I am thinking:

$start_dttm= $date.$time;

 

and then querying SELECT * FROM shuttle WHERE start_dttm = '$start_dttm'

 

however my sql record is in the datetime format of 2009-01-22 09:00:00 so I dont think this is working?

Link to comment
Share on other sites

Tushay Sir!

 

Maybe slower but will work with any well formatted date not just one with slashes. 

 

Agreed, it all depends on if the date format will always be that. If not then yes, the strtotime is better for extended functionality :)

Link to comment
Share on other sites

strtotime only works with slashes in the mm/dd/yyyy format, not dd/mm/yyyy, while the explode method can be tailored to work with either. The explode method also allows the date to be validated using checkdate

 

Also since strtotime uses mktime/Unix Timestamp it suffers from the same 1970/2038 limitations, so it cannot be used for a lot of things like birthdays.

Link to comment
Share on other sites

It is for an application only a couple people will use so speed is not so important, thanks for the feedback though.

 

Works great for me

 

Had another question though  :P

 

Is there a way that I could do a match just on the date. So if a user selects a date in the date field, I run a query that would bring out all records where the dates matched, ignoring the times?

 

ie. If My SQL Table had

id    depart_dttm

1      2009-01-22 09:00:00

2      2009-01-22 22:00:00

 

and the user entered just the date in the date field, what would I pass into my sql query to bring out both records?

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.