Click here to get back home

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

 HomeNewsGroups | Search

comp.lang.php - PHP programming language discussions 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!! oedipa 05-08-2008
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

show/hide quoted text


show/hide quoted text


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

What sounds easier to me (not tested):

show/hide quoted text
FROM banned
WHERE username = 'foo'

show/hide quoted text

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

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



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

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

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

show/hide quoted text

show/hide quoted text

show/hide quoted text

show/hide quoted text

show/hide quoted text

However, having read =C1lvaro's reply, I totally agree that this
comparison should be done completely in the SQL.

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

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy