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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question How to determine if a DATE is earlier then Now()? [PHP5]

    Simply put, I have a field in a MySQL database called expdate (of type DATE) which is the Expiry Date of the record, and ever so often I want to kick off a PHP5 function .disableExpired() that will do the following:
    1 - Get all records from Table
    2 - Check if their expdate has past based on now()
    3 - Set a status field in that case (disabled)

    Now, I know how to do (1) and (3) but honestly I've got no clue how to do (2) which is comparing the date from my table (Date field in MySQL) and NOW() in PHP5 and how to do something conditional (if) based on that ...

    So far this is what I have
    Code:
            $record = $this->db->Select("SELECT id, expdate FROM table WHERE status = 1");
    
            foreach($record as $key=>$val)
            {
                $id = $val[id];
                $expdate = $val[expdate];
    
                // ??? DO THE MAGIC HERE ??? ///
    
                if (past expiry date)
                {
                    $this->setStatus($id, '0');
                }
            }
    As you can see ... missing the key element ...

    Any help would be much appreciated...
    Thanks,

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You should be doing this from within the SQL statement, something like
    Code:
    UPDATE table
     SET status = 0
    WHERE expdate < CURRENT_DATE
    Of course this is just an example, please DO NOT run this against your db unless you know what the specific date comparison should be. I'd suggest performing a SELECT on the `id` field first to test the theory.

    Just run this statement once in the PHP script, you don't have to mess with any other comparisons. This is a perfect example of when the SQL statement should be formed correctly to avoid pulling down records and running loops against the data. SQL is more powerful than you think.

    Check the DATE and TIME functions on MySQL's site.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Although you can do this directly with PHP, I would also agree that the better option is to go by SQL using the mysql date / time functions.

    Moving to MySQL forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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