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
https://forums.phpfreaks.com/topic/143134-querying-mysql-date-using-php-variable/
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 :)

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.