View Single Post

  #3 (permalink)  
Old 05-08-2008
Álvaro G. Vicario
 
Posts: n/a
Default Re: Help with comparing date stamp in PHP to date AND time stampcoming from MYSQL db!!!!!!

oedipa@gmail.com escribió:
> $username = $_GET["username"];
> //declare all the db stuff, etc above this line
> $q = "SELECT * FROM banned WHERE username = '$username'";


What sounds easier to me (not tested):

SELECT username, IF(banned_until>NOW(), 'Y', 'N') AS still_banned
FROM banned
WHERE username = 'foo'

$is_banned = $db->f('still_banned)=='Y';

After this, $is_banned is a PHP boolean with the requested info.


Some general clues if you actually need to compare dates between PHP and
MySQL:

You can use Unix timestamps:
- In PHP, time() and many other time funcionts use/return timestamps
- In MySQL, you have FROM_UNIXTIME() and UNIX_TIMESTAMP() to do convertions
- Timestamps are long integers and can be easily compared:
if($banned_until>$now){ ... }

Or you can pass the PHP date to MySQL as a properly formatted string:
date('Y-m-d H:i:s', $foo). Again, dates can be compared without trouble:
WHERE BANNED_UTIL > NOW



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Reply With Quote