Just wondering... (php date stuff)

Discussion in 'PHP' started by Skillman13, Oct 24, 2009.

  1. #1
    I want to have a list for the next (Xbox) game release...

    And it works currently, but i want it to remove them from the list once that date has gone...

    so (example) record one has a release date of 10th november (11-10-2009)
    And it is the newest (next to be released in the whole table of records)

    So how can i make it so it goes from the list once that date has gone?

    -I don't need a coded solution as of yet,

    But im wondeirng can you give a mysql database a 'time' so once like (example) $release date < mysql time, then it goes from the list or something?

    -Is something along these lines possible?

    If not, are there any solutions?

    It would also be appricated if you share some of the code, if you do know an idea that could work. -Or how to give database a time or whatever your solution may be...

    Thanks alot,

    James
     
    Skillman13, Oct 24, 2009 IP
  2. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Sure, you'd want to create a table with a column named 'release_date' or something like that, and have it's data type set to 'DATE'. Using DATE and DATETIME are both valid options, but DATE will save space as it will only store the date in the format YYYY-MM-DD, while DATETIME will store the date AND the time as YYYY-MM-DD HH:MM:SS.

    Then, on your server, create a new cron job. Most hosting providers should provide some type of cron manager, which will make creating a cron a lot easier. If you're unsure of how they work, check out the documentation from your host, or just give the support department a call.

    Anyway, you'd set up a cron job that'd execute a PHP script every 24 hrs. This script will simply go through your database, find out which rows have a release_date less than today's date (a.k.a. yesterday), and remove em. Here's what it'd look like, assuming your table is named 'games' and the date column is 'release_date':

    cron_games.php
    <?php
    
    //Get today's date in the format YYYY-MM-DD
    $dToday = date( 'Y-m-d' );
    
    //Delete rows where release_date is yesterday.
    mysql_query( "DELETE FROM games WHERE release_date < '$dToday'" );
    
    ?>
    PHP:
    I did it this way instead of getting the actual date of yesterday just for smaller code.
     
    DollaradeConner, Oct 24, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    yeah, you can do select query such that all the records prior to today or a specified date won't be shown.

    Please share table structure to help building exact query.
     
    mastermunj, Oct 24, 2009 IP
  4. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I beat you munj :)
     
    DollaradeConner, Oct 24, 2009 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    DollaradeConner, I am glad to have someone take my place when I'm not around or busy ;)
     
    mastermunj, Oct 24, 2009 IP
  6. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Wow, thank you very much conner, I know have it working :)

    And :p ha, better luck next time mastermunj, well I hope there not a next time...
     
    Skillman13, Oct 24, 2009 IP