View Single Post

  #6 (permalink)  
Old 05-08-2008
Captain Paralytic
 
Posts: n/a
Default Re: Help with comparing date stamp in PHP to date AND time stampcoming from MYSQL db!!!!!!

On 8 May, 09:34, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 8 May, 09:14, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
>
>
> > On 8 May, 05:21, oed...@gmail.com wrote:

>
> > > 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");

>
> > > 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.

>
> > > $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

>
> > > 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.

>
> > > //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

>
> > > }

>
> > > 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

>
> > This is so simple:

>
> > $q = "SELECT username, DATE_FORMAT(banned_until,'%Y-%m-%d') FROM
> > banned WHERE username = '$username'";

>
> Oops missed a bit:
> $q = "SELECT username, DATE_FORMAT(banned_until,'%Y-%m-%d')
> banned_until FROM banned WHERE username = '$username'";


However, having read Álvaro's reply, I totally agree that this
comparison should be done completely in the SQL.
Reply With Quote