Jump to content

"Take down" Image after Time Expires


louisstephens
 Share

Recommended Posts

So I have an odd feature to tackle. I want to include an image in the template (aka a header), but want to implement a function to set a time in the future. Once the time was met, the image will automatically come down (if possible switch to another image, but that is not something Ill worry about now). 

Now, I set up an image field "timed_image", and I set up the date/time field "select_timed_expire". I also know how to echo the information on the page, but where I am finding issue is actually how to implement this. I could use javascript and pass the date/time to it, but from there I am quite lost on how to implement this.

Has anyone done something like this before?

Link to comment
Share on other sites

Something like this?

$now = date("Y-m-d h:i:s");
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.

if ($now < $expires){
      echo "<img src='{$page->timed_image->url}'/>";

} else {
     // do something else or nothing.
}
  • Like 2
Link to comment
Share on other sites

Same solution as Reno, but I'd compare the timestamps and not date strings :)

$now = time();
$expires = strtotime($page->select_timed_expire); // If you have a date string as output format

  • Like 2
Link to comment
Share on other sites

If you want to cycle to the next image after the expiration, you can use a multi-image field and show the first image until the expiration date, and then move on to the second image.

$now = date("Y-m-d h:i:s");
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.
$url = $page->timed_image->first()->url; // default to first image

// if it's past the expiration, get the 2nd image. 0 based index, so 1 is 2nd image.
if ($now > $expires) $url = $page->timed_image->eq(1)->url; 

echo "<img src='$url'/>"; 
Link to comment
Share on other sites

I had just found out about using first();. but had a quick question. Most of the pages will have the default single image as the header, but once in awhile I will need to run a different image in its' place (set to expire). Will the above example cause an issue on the pages that only have the one image and nothing before it in the array?

Link to comment
Share on other sites

You would just need to check if there is an expiration set, if there isn't, then just stick with the first image. If there is an expiration set, then check the dates and respond accordingly.

untested, written in browser.

$url = $page->timed_image->first()->url; // default to first image, make sure your image field is set to multiple.
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.

if ($expires){
    if (date("Y-m-d h:i:s") > $expires){
        $url = $page->timed_image->eq(1)->url; // if it's past the expiration, get the 2nd image.
    }
}

echo "<img src='$url'/>"; 
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...