Page 1 of 1

user defined tag not working [solved]

Posted: Wed Jan 07, 2015 3:18 pm
by babel
I have this udf tag

Code: Select all

function seizoen() {
    date_default_timezone_set('UTC');
   $limits=array(
   '/12/21'=>'<img src="images/winter.jpg"/>',
   '/09/21'=>'<img src="images/herfst.png" alt="moeder met kind in herfstlandschap in de bergen"/>',
   '/06/21'=>'<img src="images/zomer.jpg"/>',
   '/03/21'=>'<img src="images/lente.jpg" alt="wandelaar met rugzak"/>',
   '/12/31'=>'<img src="images/winter.jpg"/>');
   foreach($limits AS $key => $value) {
       $limit=date("Y").$key;
       if(strtotime("now")>strtotime($limit)) {
           return $value;
       }
   }
}
 
echo seizoen();
The purpose was to show another background image according to the season. Worked fine, but not anymore. Nothing to my knowlodge has changed.
In the template you find <div id="bg_index">{img_seizoen}</div>
It's for cms ms version 1.11.9.
The website ishttp://www.slee-buitensport.nl/index.php
Can somebody tell me what is wrong? Just for the sake. My knowlodge of PHP is very limited.
Thanks for reading.

Re: user defined tag not working

Posted: Thu Jan 08, 2015 10:20 am
by Jeff
According to that logic, none of them will be selected because none of the "limits" are in the past.

If I understand the pattern/logic, the last limit should be /01/01 instead of /12/31.

Re: user defined tag not working

Posted: Thu Jan 08, 2015 11:59 am
by Rolf
Where did you find the original code?

Re: user defined tag not working

Posted: Thu Jan 08, 2015 5:01 pm
by babel
Thanks Jeff for the help. Can't remember where I found the code, but your solution worked. Great.

Re: user defined tag not working [solved]

Posted: Thu Jan 08, 2015 7:18 pm
by Rolf
I rewrote the UDT a bit. It is more clean this way...

UDT named "season"

Code: Select all

date_default_timezone_set('UTC');

$limits = array (
  '/12/21'=>'winter',
  '/09/21'=>'autumn',
  '/06/21'=>'summer',
  '/03/21'=>'spring',
  '/01/01'=>'winter');

foreach ($limits as $key => $value) {
  $limit = date('Y') . $key;
  if ( time() >= strtotime($limit) ) return $value;
}
In page or template:

Code: Select all

<img src="{uploads_url}/images/{season}.jpg" alt="{season}" />

Re: user defined tag not working [solved]

Posted: Thu Jan 08, 2015 8:13 pm
by calguy1000
instead of strtotime('now') just use time()