Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!!

Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!!

Post by Lars Eighn » Fri, 09 May 2008 16:15:31


In our last episode,
< XXXX@XXXXX.COM >,
the lovely and talented XXXX@XXXXX.COM
broadcast on comp.lang.php:







try echo $banned_until here to see if you really are getting a value.

The answer probably is that you are getting a string, not time, which
is expressed in an integer number of seconds.




This is because $banned_until = 0. I suspect your problem is not
in date but that you are not what you think from the database.



of course you could make date2 = date('Y-m-d h:m:s'); which evidently is the
format you are getting from the database. This of course has the
disadvantage of being obviously correct.



Aha! The plot thickens!




Okay. date wants the second parameter to be a number for time, a big-ass
integer in this case. What you are getting from the database is a string.
How to change a string to time? How? How?

$date1 = date('Y-m-d',strtotime($banned_until));

Note: the above contains *two* hints at *different* solutions. If you do
both of them, it won't work. Pick one.




--
Lars Eighner < http://www.yqcomputer.com/ ; XXXX@XXXXX.COM
Countdown: 257 days to go.
 
 
 

Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!!

Post by varo G. Vi » Fri, 09 May 2008 16:50:56


XXXX@XXXXX.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_unti>>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_unti>>$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://www.yqcomputer.com/ - varo G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci web: http://www.yqcomputer.com/
-- Mi web de humor al ba Mar: http://www.yqcomputer.com/