Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Date question in php?

    What I am trying to do is compare dates using php and a mysql database. A date taken from my mysql database and compare it with today's date, and if today's date is 14 days more then the date taken from the database, then for it to echo a response message on a webpage, or if it less then I need it to echo a certain response message on a webpage. Please tell me if this code will work, if not please help me fix it, please?




    <?php

    $CurDate = date("Y").date("d").date("m");

    // Make a MySQL Connection
    mysql_connect("localhost", "my_username", "my_password") or die(mysql_error());
    mysql_select_db("my_database") or die(mysql_error());

    $SignupDate_A = mysql_query("SELECT pdate FROM my_table") or die(mysql_error());

    $SignupDate_B = strtotime($SignupDate_A);
    $SignupDate = $SignupDate_B + 14;

    if ($CurDate <= $SignupDate)
    {
    echo "You are below the expiration date";
    }
    elseif ($CurDate > $SignupDate)
    {
    echo "The Date has expired";
    }

    ?>



    Thanks!
    Last edited by ArcGames; 06-21-2006 at 08:26 PM.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Don't bother doing the date calculations from php. Do them directly in mysql.
    Look up the use of DATE_SUB and DATE_ADD to get what you want and only return the relevant data from the database rather than all data.

    your query will be significantly faster if you do it that way.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I need to use php as I am using it on a website and also I am fairly new to php and mysql, could you please just fix my code to have it do what I am wanting, please?

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    You would use something like this to check the dates in mysql:

    Code:
    SELECT pdate FROM my_table where adddate(pdate, interval 14 day) < now()
    that says to add 14 days to each pdate and only return those that are older than that.

    Note that your query above would return every row in your table since you have no where condition. are you trying to compare a pdate for one specific user? if so you would add a condition for that in the query above.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •