Comparing Two Dates Using PHP from MYSQL Database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ameshkin

    Comparing Two Dates Using PHP from MYSQL Database

    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($qu ery) or die('Query failed: ' . mysql_error());
    $row = mysql_fetch_ass oc($result);


    // Free resultset
    //mysql_free_resu lt($result);

    // Closing connection
    mysql_close($li nk);



    $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($qu ery2) or die('Query failed: ' .
    mysql_error());
    $row2 = mysql_fetch_ass oc($result2);


    // Free resultset
    //mysql_free_resu lt($result2);

    // Closing connection
    mysql_close($li nk2);

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


    $LastComment = $row2['timestamp'];

    //CONVERT TIMESTAMP INTO a date form
    $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,
    $day,
    $year);
    $convertedTime = date("Y n d", $timestamp);
    strtotime('$con vertedTime');
    echo $convertedTime;
    ECHO "<BR>";

    strtotime('$Las tLogin');

    echo $LastLogin;


    if (strtotime('$co nvertedTime') > strtotime('$Las tLogin')) {
    echo "New Comments";
    } else {
    echo "There are no new comments";
    }

    ?>

  • milahu

    #2
    Re: Comparing Two Dates Using PHP from MYSQL Database

    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.

    Comment

    • ameshkin

      #3
      Re: Comparing Two Dates Using PHP from MYSQL Database

      I will try the unix timestamp.

      So what you're saying though is, there is no need for mysql_close
      ($link)

      ????

      Comment

      Working...