Page 1 of 1

[solved] timebased content

Posted: Thu Feb 16, 2012 11:25 am
by HarmO
Im working on a website that needs timebased content. (only visible between 18:00 and 20:00 for example)

i have the PHP code for a UDT, but the owner of the site is a simple user and must be able to adjust this content so i want to use a GCB where he can modify the content

Code: Select all

//vars
$date = getdate();
$hour = $date['hours'];

//code
if ($hour >= 18 && $hour <=20) {
	echo ('in time'); //this should be replaced by a GCB
}
else {
	echo ('out of time'); //this should be replaced by a GCB
} 
Who knows how i need to proceed?

Re: timebased content

Posted: Thu Feb 16, 2012 12:07 pm
by uniqu3
You don't need a UDT, you can use $smarty.now for this like:

Code: Select all

{if $smarty.now|date_format:'%H' >= '18' && $smarty.now|date_format:'%H' <= '20'} 
    do something
{else}
    something else
{/if}
or with your UDT (lets say yo named it is_time):

Code: Select all

//vars
$date = getdate();
$hour = $date['hours'];

//code
if ($hour >= 18 && $hour <= 20) {
   $time = true;
}
else {
   $time = false;
}

$smarty = cmsms()->GetSmarty();
$smarty->assign('time', $time);
and in template:

Code: Select all

{is_time}
  {if ($time == true)}
      do something
  {else}
      do something else
  {/if}

Re: timebased content

Posted: Thu Feb 16, 2012 12:35 pm
by Jos
In addition to what Uniqu3 mentioned, you can even make it more simple for the editor-user .

In a few days I will release a new version of the "Custom Global Settings" module. http://dev.cmsmadesimple.org/projects/customgs

You can define your own fields there, like 'starttime' and 'endtime'

You can create a page template, which contains this code:

Code: Select all

{CustomGS}
{if $smarty.now|date_format:'%H:&i' >= $CustomGS.starttime && $smarty.now|date_format:'%H:i' <= $CustomGS.endtime}
    {content}
{else}
    {content block="SiteClosedContent"}
{/if}
By putting this in a page template, the user can not mess around with the code..
He can simply edit the page, which has two content fields.
He is also able to edit the openingtime if he wants to, by editing the field in the Custom Global Settings.

Re: timebased content

Posted: Thu Feb 16, 2012 2:29 pm
by HarmO
Thx,

for now i wil use your code uniqu3,

Code: Select all

{if $smarty.now|date_format:'%H' >= '18' && $smarty.now|date_format:'%H' <= '20'}
    {content}
{else}
    {content block="SiteClosedContent"}
{/if}
but i'm curious to see you module Jos, because i imagine that the website owner maybe want to modify the timelap and start 30 minutes later...

Re: timebased content

Posted: Thu Feb 16, 2012 3:23 pm
by Wishbone
Jos wrote:In a few days I will release a new version of the "Custom Global Settings" module. http://dev.cmsmadesimple.org/projects/customgs
hahaha! I was working on a module called SiteSettings that does something similar. Even though 75% complete, I abandoned it because I could accomplish something similar with content blocks on the home template, accessable through CGSimpleSmarty.

Looking forward to seeing it.

Re: [solved] timebased content

Posted: Thu Feb 16, 2012 7:33 pm
by HarmO
made a small change to make it work with minutes:

Code: Select all

{if $smarty.now|date_format:"%H:%M" >= '18:00' && $smarty.now|date_format:"%H:%M" <= '20:45'}
    {content}
{else}
    {content block="SiteClosedContent"}
{/if}