[solved] timebased content

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
HarmO
Power Poster
Power Poster
Posts: 251
Joined: Thu Jan 26, 2012 3:22 pm

[solved] timebased content

Post 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?
Last edited by HarmO on Thu Feb 16, 2012 4:58 pm, edited 1 time in total.
uniqu3

Re: timebased content

Post 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}
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: timebased content

Post 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.
HarmO
Power Poster
Power Poster
Posts: 251
Joined: Thu Jan 26, 2012 3:22 pm

Re: timebased content

Post 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...
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: timebased content

Post 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.
HarmO
Power Poster
Power Poster
Posts: 251
Joined: Thu Jan 26, 2012 3:22 pm

Re: [solved] timebased content

Post 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}
Post Reply

Return to “The Lounge”