Querying a mysql text for dates
Moderator: General Moderators
-
sirstrumalot
- Forum Commoner
- Posts: 27
- Joined: Mon May 18, 2009 10:26 pm
Querying a mysql text for dates
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
I'm not entirely sure what you mean by utility?
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).
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];
?>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.
Last edited by AlanG on Wed Sep 23, 2009 11:29 am, edited 2 times in total.
-
sirstrumalot
- Forum Commoner
- Posts: 27
- Joined: Mon May 18, 2009 10:26 pm
Re: Querying a mysql text for dates
That looks perfect. Thanks!