Originally posted by HairyArse
Kubis, can you explain what that code does?
Sure.
CASE WHEN condition THEN result1 ELSE result2 END is the same as switch() in PHP so the query means:
if datecol1 is bigger datecol2 then return 1; if not, return 0 as column named datecol1_is_bigger.
with:
SELECT datecol1, datecol2, CASE WHEN datecol1 > datecol2 THEN 1 ELSE 0 END AS datecol1_is_bigger FROM tablename
you will get a row with 3 columns. datecol1 (is a date), datecol2 (is a date) and datecol1_is_bigger (returns 0 or 1 as a result of the two dates comparation)
If you need to pass the column dates to PHP and the database engine is MySQL, you can use:
select UNIX_TIMESTAMP(datecol1) AS datecol1uts, UNIX_TIMESTAMP(datecol2) AS datecol2uts....
using the unix_timestamp() function the mysql converts date representation to unix timestamp (also the same kind of date representation you get when you use time() function in PHP).
in the case you are not using mysql, but for example postgres or oracle, use to_char(), like to_char(datecol1, 'YYYY-MM-DD HH24:MI:SS') AS datecol1date; then in PHP use strtotime($result['datecol1date']) and voila 😉