View Single Post

  #2 (permalink)  
Old 05-08-2008
Lars Eighner
 
Posts: n/a
Default Re: Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!!

In our last episode,
<76698201-9d3d-4f13-9f34-a4ca376dc8ce@l64g2000hse.googlegroups.com>,
the lovely and talented oedipa@gmail.com
broadcast on comp.lang.php:

> Hi all-


> I've searched for at least an hour tonight before posting this
> question into here. It's one of those questions that seemed simple AT
> FIRST, but I'm having a hard time filtering the info I get on all the
> date formats and how to compare them for my little script.


> This script is to disallow a user into an area who has been banned for
> a certain amount of time. So when the user logs in again, we compare
> the current date to the one the admin put into MYSQL and come up with
> an answer (that it is the date they are allowed back in or not).
> Also, I have no control over how the data is inserted on my part,
> since another company is doing it. So I can't change the date format
> on that end.


> Here is the code I'm working with so far:


> $username = $_GET["username"];
> //declare all the db stuff, etc above this line
> $q = "SELECT * FROM banned WHERE username = '$username'";
> $db->query($q);
> $db->next_record();


> $banned_user = $db->f("username");
> $banned_until = $db->f("banned_until");


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.

> if ($db->num_rows() > 0) {



> //$date1 = date("Y-m-d",$banned_until); this returns a datestamp
> of 1969-12-31 instead of 2008-05-05 so instead I'm back to the line
> below which just pulls the banned_until date AND timestamp from the
> db.


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.

> $date1 = $banned_until;


> $date2 = date('Y-m-d'); //this gives me exactly what I want. I
> just want the banned_until to match the format


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.

> echo "$date1 compare to $date2";


> //this returns a date string that currently looks like: 2008-05-05
> 18:11:19 compare to 2008-05-08. I can't compare this currently.


Aha! The plot thickens!

> //Solution??? I want to remove the timestamp and just work with
> comparing the date in Y-m-d format to see if they are equal or not.


> } else {


> echo $banned_until; //simply displays the banned_until stamp which
> is 2008-05-05 18:11:19


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.

> }


> So how do I normalize the two date stamps and then compare them
> properly to allow a user back in?


> Many thanks for ANY help on this!!!!!!!!


> -Oedipa Maas

--
Lars Eighner <http://larseighner.com/> usenet@larseighner.com
Countdown: 257 days to go.
Reply With Quote