Page 1 of 1
How can I compare MySQL/PHP dates?
Posted: Tue Apr 05, 2005 6:07 pm
by bredoteau
Hi,
is there a simple way of comparing MySQL-dates with PHP-ones?
When I query MySQL I receive a string like 2005-04-05 (does it depend on the locale settings or can I use substr to retrieve data??).
I produces dates like 05. 04. 2005 with PHP. Comparing those does not work. Are there any useful functions for this?
Posted: Tue Apr 05, 2005 6:19 pm
by feyd
how do you want to compare them, specifically? Comparing if they are equal, you can do a lot.. have MySQL use DATE_FORMAT() to change the for it returns it, or (MySQL) UNIX_TIMESTAMP() to generate a unix timestamp of the date... or you can have php use MySQL's format..
If you want to comare if A is more recent than B, then you need to use MySQL's format, or something similar, as that accurately allows math-like comparison.
Posted: Wed Apr 06, 2005 2:42 pm
by bredoteau
As I need to construct a MySQL query I cannot use MySQL-side functions.
I didn't manage to convert the data via any php function like strtodate.
But it works now, I just did it "by hand":
Code: Select all
//$min/$max are strings of the format "e;01. 04. 2005"e;
$tempmax = substr($search_arrayїmax_date],7,5)."e;-"e;.substr($search_arrayїmax_date],4,2)."e;-"e;.substr($search_arrayїmax_date],0,2);
$tempmin = substr($search_arrayїmin_date],7,5)."e;-"e;.substr($search_arrayїmin_date],4,2)."e;-"e;.substr($search_arrayїmin_date],0,2);
sql = "e;SELECT * FROM table WHERE date BETWEEN $tempmin AND $tempmax;
feyd | Please review how to post code using Code: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Apr 06, 2005 5:17 pm
by timvw
untested...
Code: Select all
$part = explode('.', $min);
$mysql_date = "{$part[2]}-{$part[1]}-{$part[0]}";
meaby some trimming will be needed too...