By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Comparing Two Dates Using PHP from MYSQL Database

P: n/a
Hi GUys,
Im trying to compare two dates in MYSQL. But its not treating the
dates as numbers, but as strings. I try using strtotime but that did
not work.

Basically, if the last comment is newer than the last user login, then
I need for a message to pop up and say New Comments.

If not, then no new comments. The area of this code which is not
working is at the bottom.

Does anyone know how to figure out how to compare these two dates?

mysql_select_db('playlist') or die('Could not select database');
// Performing SQL query
$query = "SELECT *
FROM `Login`
WHERE `loginName` = '$logname'
ORDER BY `loginTime` DESC ";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_assoc($result);
// Free resultset

// Closing connection

$LastLogin = $row['loginTime'];

//second task is to pull last comment

$link2 = mysql_connect('localhost', 'premium', 'amir18')
or die('Could not connect: ' . mysql_error());

mysql_select_db('premium') or die('Could not select database');
// Performing SQL query
$query2 = "SELECT *
FROM `comments`
WHERE `owner` = '$logname'
ORDER BY `timestamp` DESC ";
$result2 = mysql_query($query2) or die('Query failed: ' .
$row2 = mysql_fetch_assoc($result2);
// Free resultset

// Closing connection

echo "<br>";
//echo $row2['timestamp'];
$LastComment = $row2['timestamp'];

$date = $row2['timestamp'];
$year = substr($date,0,4);
$month = substr($date,4,2);
$day = substr($date,6,2);
$hours = substr($date,8,2);
$minutes = substr($date,10,2);
$seconds = substr($date,12,2);
$timestamp = mktime($hours, $minutes, $seconds, $month,
$convertedTime = date("Y n d", $timestamp);
echo $convertedTime;
ECHO "<BR>";


echo $LastLogin;
if (strtotime('$convertedTime') > strtotime('$LastLogin')) {
echo "New Comments";
} else {
echo "There are no new comments";


Apr 7 '06 #1
Share this Question
Share on Google+
2 Replies

P: n/a
You can convert mySQL timestamps to UNIX timestamps using mySQL's
UNIX_TIMESTAMP() function and simply compare them in PHP.

When using a mySQL database, you should connect to the database server
once and select the needed database. Then you can perform your queries.
The connection to the server is closed automatically after the script
has finished.

Apr 7 '06 #2

P: n/a
I will try the unix timestamp.

So what you're saying though is, there is no need for mysql_close


Apr 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.