Php Mysql date comparison

Posted on 2011-10-16
Last Modified: 2012-05-12
Hi Experts

I have written the following statement that checks todays date against a date in our database.
If todays date 2011-10-16 is greater than database date 2099-10-19 then update access to lock account and redirect to page with instructions.

For some reason no matter what I do with the greater than or smaller than the update statement fire each time locking the account.

I only want the statement to fire if the date today is greater than the subscription date.

Am I doing the comparison correctly?

if(date("Y-m-d") > $row_rs_DWS_access_check['access_end_date']){
$sqlUpdateAccess = "update usrprofiles set usrprofiles.Active=0 where usrid = ".$_SESSION['kt_login_id'];
$result = mysql_query($sqlUpdateAccess);
header("Location: access.control.account.locked.php");

Open in new window

Question by:matthewdacruz
    LVL 7

    Expert Comment

    to compare date you need to use
    strtotime - a php function

    LVL 7

    Expert Comment

    LVL 34

    Accepted Solution

    kshna said: "to compare date you need to use strtotime - a php function"

    That is not strictly true. If dates are in the format YYYY-MM-DD then they can be compared directly with no need for other functions, but if the dates are in another format then strtotime is useful for converting them to integers for direct comparison.

    There is another 'but' as well. strtotime will not work for any date past 2038 so the example given - 2099 - would fail with strtotime but would work if done as a string in YYYY-MM-DD format.

    The newer PHP DateTime class is more likely to help as it does not have the 2038 restriction


    Author Closing Comment

    Thank you for your direction, it steered me to the best solution for what I was working on.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now