SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looking For Simple Date Expiration Code

    This is for a .php page and I'm basically looking to have a certian text to show for 5 days only of the date it was made/posted. For example if I would type in 08-26-07 and it would display the text until 08-31-07. What would be the best way to do this? I don't have much experience in this but I figured it would be something simple.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look up timestamps, like the function time() and then set a timpestamp to your content. Then check to see if the current timestamp MINUS the timestmap set is greater than the amount of time you'd like the content to be up for, if so, then drop it.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where can I find an example of this? I tried searching around but found nothing. I'm new at this.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a quick, pseudocodish example. You couldn't just drop it in but it works:
    Code:
    // $current_content_time is the content's timestamp (set with time())
    $time_now = time();
    $time_5_days_ago = $time_now - (60 * 60 * 24 * 5);
    
    if ($current_content_time < $time_5_days_ago)
    {
    // Over 5 days ago
    }
    else
    {
    // Within 5 days
    }
    The math in there is 60 seconds times 60 minutes times 24 hours times 5 days (amount of seconds in 5 days).
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  5. #5
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay this is what I am looking for. Quick question however regarding $current_content_time

    Let's use today as an example, it's 08/26/07. Would I put $current_content_time = "08/26/07"; ?

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shockbotkins View Post
    Okay this is what I am looking for. Quick question however regarding $current_content_time

    Let's use today as an example, it's 08/26/07. Would I put $current_content_time = "08/26/07"; ?
    No, for the current time you would do:
    Code:
    $current_content_time = time();
    Since it is a timestamp.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  7. #7
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Won't things always display though because I don't see how it would expire without me inputing a certian date. What I'm wanting to do is I place a image button on a .php webpage and 5 days from now it shouldn't show without me taking it off.

  8. #8
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I some how managed to get it to work on my own after looking over some things on php.net documentation. Let me know if what I did makes sense.

    PHP Code:
    <?php

    $date1 
    mktime(0,0,0,08,26,2007) - mktime(0,0,0,09,01,2007);
    $date2 "432000";


    if (
    $date1 $date2)
    {
    echo 
    "Expired";
    }
    else
    {
    echo 
    "Not Expired";
    }
    ?>

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Close but no... not quite. Okay a timestamp is a 10-digit integer representing the seconds passed since Jan 1, 1970. (It will become an 11 digit integer in 2027, i believe, or something like that) So when you call time() it returns the amount of seconds AT THAT MOMENT, since Jan 1, 1970.

    mktime() makes that up also, so you could use that. Here is something that would actually work:

    Code:
    $then = mktime(0,0,0,8,26,2007);
    $today = time();
    $difference = $today - $then;
    
    if ($difference > 432000)
    {
    expired
    }
    else
    {
    show it
    }
    The problem with your code is that $data1 and $date2 are both constant, so you will always get the same result. Additionally $date1 is a negative integer
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  10. #10
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, works perfect. Thanks a lot for the help, I appreciate it.


Bookmarks

Posting Permissions

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