1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Setting an Expiration Date with PHP - How?

Discussion in 'PHP' started by GeorgeB., Apr 27, 2007.

  1. #1
    OK I store the publish date of user submitted content in the DB like this:

    0000-00-00 which is basically date(Y-m-d);

    I want to give the user the option to let it expire

    24 hours later
    1 week later
    1 Month later

    How would you go about setting an expiration date to store in the DB alongside the published date?
    SEMrush
    I can change the format I store the date if need be. I'd imagine I need to add an actual time stamp for the 24 hour part right?

    Of course Green Rep for anyone who even tries to help :)
     
    GeorgeB., Apr 27, 2007 IP
    SEMrush
  2. clancey

    clancey Peon

    Messages:
    1,100
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would add simply an expiration date time value in seconds to the table. When the user selects an expiry period, I would convert that to seconds. I would add that the published date (in seconds) and enter that in the expiration date cell. Then, when displaying articles, I would check to see whether it has expired. If the current time stamp value (in seconds) is greater than the value in that cell than it has expired, otherwise it has not.

    If you cannot change that table, then you would create another one which contains the ID for the story and the expiry time. You would then make the comparison, searching for records which match the story ID and where the expiry time is greater than the current time
     
    clancey, Apr 27, 2007 IP
    GeorgeB. likes this.
  3. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,696
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #3
    How code I do that in seconds though?

    And hwo would I store the original timestamp in the DB so it could accurately be read and calculated? Y-m-d H-m-s ?
    Maybe something like

    $today = date("Y-m-d", time());

    // Define timestamp for expiration time
    $exp_time = time()+86400;

    That would be 24 hours so then I could store the expiration stamp like $exp = date("Y-m-d", $exp_time); ?

    1 week = 604800 seconds
    1 month = 2629743.83 seconds
     
    GeorgeB., Apr 27, 2007 IP
  4. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #4
    hi George,
    you can easily store the result of the php function time() in a timestamp field in the database.
    Then for expiry, a cronjob for a php file which checks for expired items.
    This crojob would do:
    - $time = time();
    - $limit = $time - (24*60*60); if 24 hours is chosen
    - then you query the database for entries where the timestamp field is less than $limit
     
    legend2, Apr 27, 2007 IP
    GeorgeB. likes this.
  5. clancey

    clancey Peon

    Messages:
    1,100
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #5
    'Y-m-d' is not a problem.

    An article expiring in one week would have the expiry time stamp:

    $expiry = date("Y-m-d", time() + 7*24*60*60 );

    while an article expiring in one month might have the expiry time stamp:

    $expiry = date("Y-m-d", time() + 30*24*60*60 );

    You would then store the expiry date.

    When the page is being built get today's date stamp:

    $today = date("Y-m-d", time());

    Then do the comparison:

    if( $today > $expiry) 
       {
       ## ignore expired article
       }
    else
       {
       ## display article
       }
    PHP:
    You could also extend legend2's idea and have a cron job go through the article database at 12:01 AM local time and mark expired articles as inactive. Then you would only display "active" articles.
     
    clancey, Apr 27, 2007 IP
  6. AleckProg

    AleckProg Guest

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    // modify this setting your expiry time
    $expiry = date("Y-m-d", time() + 30*24*60*60 );
    
    $today = date("Y-m-d", time());
    
    if( $today > $expiry)    {
       ## ignore expired article
       }
    else
       {   
    ## display article
       }
    
    PHP:
     
    AleckProg, Apr 27, 2007 IP
    GeorgeB. likes this.
  7. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,696
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #7
    Thanks everyone, green given.
     
    GeorgeB., Apr 27, 2007 IP