|
Posted by oedipa on May 8, 2008, 1:21 am
Please log in for more thread options
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'";
show/hide quoted text
$db->query($q);
$db->next_record();
show/hide quoted text
$banned_user = $db->f("username");
$banned_until = $db->f("banned_until");
show/hide quoted text
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
|
|
Posted by =?ISO-8859-1?Q?=22=C1lvaro_G=2 on May 8, 2008, 3:50 am
Please log in for more thread options
oedipa@gmail.com escribió:
show/hide quoted text
> $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):
show/hide quoted text
SELECT username, IF(banned_until>NOW(), 'Y', 'N') AS still_banned
FROM banned
WHERE username = 'foo'
show/hide quoted text
$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:
show/hide quoted text
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:
show/hide quoted text
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 --
|
|
Posted by Captain Paralytic on May 8, 2008, 5:14 am
Please log in for more thread options On 8 May, 05:21, oed...@gmail.com wrote:
show/hide quoted text
> 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'";
|
|
Posted by Captain Paralytic on May 8, 2008, 5:34 am
Please log in for more thread options show/hide quoted text
> 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'";
|
|
Posted by Captain Paralytic on May 8, 2008, 6:40 am
Please log in for more thread options show/hide quoted text
> > 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=
show/hide quoted text
> > > 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=
show/hide quoted text
> > > 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 =3D $_GET["username"];
> > > //declare all the db stuff, etc above this line
> > > $q =3D "SELECT * FROM banned WHERE username =3D '$username'";
> > > $db->query($q);
> > > $db->next_record();
> > > $banned_user =3D $db->f("username");
> > > $banned_until =3D $db->f("banned_until");
> > > if ($db->num_rows() > 0) {
> > > //$date1 =3D date("Y-m-d",$banned_until); this returns a datestamp=
show/hide quoted text
> > > 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 =3D $banned_until;
> > > $date2 =3D 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=
show/hide quoted text
> > > 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=
show/hide quoted text
> > > 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 =3D "SELECT username, DATE_FORMAT(banned_until,'%Y-%m-%d') FROM
> > banned WHERE username =3D '$username'";
> Oops missed a bit:
> $q =3D "SELECT username, DATE_FORMAT(banned_until,'%Y-%m-%d')
> banned_until FROM banned WHERE username =3D '$username'";
However, having read =C1lvaro's reply, I totally agree that this
comparison should be done completely in the SQL.
|
| Similar Threads | Posted | | Date time stamp help | February 10, 2005, 11:56 am |
| date and time stamp | August 20, 2005, 2:06 am |
| How to carry out time stamp subtraction in MySQL? | May 31, 2009, 9:19 pm |
| Convert stored mysql DateTime of UTC datetime stamp into another time zone | December 2, 2008, 2:48 pm |
| unix time stamp as float instead of integer? | May 23, 2005, 4:50 pm |
| convertint time string to time stamp | July 9, 2004, 4:02 pm |
| Manipulating date with "00" in it coming from mySQL | May 13, 2009, 12:05 pm |
| Help: Date function not returning correct date & time | May 28, 2005, 11:59 am |
| Need helps with date and time in PHP/MySQL | November 20, 2006, 9:04 am |
| php to get date and time separately into MySQL? | May 17, 2008, 7:17 pm |
|
$db->next_record();