Page 1 of 1

Querying a mysql text for dates

Posted: Wed Sep 23, 2009 10:14 am
by sirstrumalot
I have a database table that took a date in the VARCHAR format as 'MM/DD/YYYY'. I am using a utility that uses the date as three seperated fields- day, month, and year. How do I query the date from my existing database and make the variables for day, month and year equal to the values within the preformatted field?

Re: Querying a mysql text for dates

Posted: Wed Sep 23, 2009 11:20 am
by AlanG
I'm not entirely sure what you mean by utility?

Code: Select all

<?php
$date = '01/01/2001';
$parts = explode('/',$date); // Breaks the $date up and puts it into an array
 
$day = $parts[0];
$month = $parts[1];
$year = $parts[2];
?>
That would break it up, you can pretty much do what you want with it now, such as put it into some database fields etc...

Update

Sorry, re-read your question. So the date is stored in the database as a varchar but the form inputs are broken into 3 fields (day, month and year).

  • You can do what I did above which is kinda messy and compare each value separately.
    You could combine the 3 fields and convert it to a unix timestamp, then convert it to the proper date format (same as the database) and compare.
    You could combine the 3 fields and the database date to a unix timestamp and compare.
    You could just format the 3 fields into DD/MM/YYYY and compare with the database string. Just remember that it is a string comparison so as long as your not checking which date came first etc... you should be ok.

Re: Querying a mysql text for dates

Posted: Wed Sep 23, 2009 11:28 am
by sirstrumalot
That looks perfect. Thanks!