Forum Moderators: coopster
I have to run a query in mysql where records from a date range are selected. I need to know how to make the query of following type:
select * from datetest
where date between '11-11-2001' AND '01-10-2009'
order by date desc
It will be great if someone can tell me which is the best format for dates:
d/m/y
d-m-y
dmy
12-june-2001
12-06-2001
12-06-01
I am so confused,
Thanks for help
Jaunty
What you can do however is to change the format with php:
$months = array("January" => "01", "February" => "02", ... "December" => "12");
$sql = "ALTER TABLE datetest ADD data DATE AFTER date;
mysql_query($sql);$sql = "SELECT id, date FROM datetest";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$fields = explode("-", $row['date']);
$month = fields[1];
$data = $fields[2] . "-" . $months[$month] . "-" . $fields[0];$sql = "UPDATE datetest SET data='$data' WHERE id='". $row['id']."'";
mysql_query($sql);
}
$sql = "ALTER TABLE datetest DROP COLUMN date";
mysql_query($sql);$sql = "ALTER TABLE datetest CHANGE data date DATE";
mysql_query($sql);
However if I were you I wouldn't use date for a field name, as it's a reserved word.
Best regards
Michal Cibor