Date and Time rules

(15 votes, average 4.73 out of 5)

MetaMod has great support for dates and times. You can make simple rules that publish and unpublish modules at certain dates and times, but that’s just the beginning. Rules like these are great for displaying certain modules during “shopping hours”, radio programme broadcasts, sports seasons etc.

Using JomGenius

This technique is available from MetaMod 2.4 onwards. It greatly simplifies recurring time periods. See here for full documentation on this feature.

Make sure you have set the Time Zone dropdown in MetaMod to your local time zone if you’re relying on time/date related functions.

Any of the following rules can be used independently of the others.
if ( $core_genius->inTimeSpan( "may" ) ) return XXX; /* month of May every year */
/* 1st Feb to 24 May every year */
if ( $core_genius->inTimeSpan("Feb 01 - May 24" ) ) return XXX; 
/* 7:00-9:00 every Sunday */
if ( $core_genius->inTimeSpan( "sun 07:00 - sun 09:00" ) ) return XXX;
/* every Saturday and Sunday */
if ( $core_genius->inTimeSpan( "sat - sun" ) ) return XXX; 
/* 6PM Friday to midnight Sunday night (start of monday) */
if ( $core_genius->inTimeSpan( "fri 18:00 - mon 00:00" ) ) return XXX; 
if ( MM_DAY_OF_MONTH <= 7 ) return XXX; /* 1st to 7th day of every month */
// no explicit JomGenius support for days of the month without the month specified
// between 9-11AM, 4-6PM and 8-10PM Monday to Friday 
if ( $core_genius->inTimeSpan("mon - fri" )
   and $core_genius->inTimeSpan( "09:00 - 11:00; 16:00 - 18:00; 20:00 - 22:00" ) ) {
 return XXX;
}

Using Constants

If you have MetaMod 2.4 or more recent, try the "JomGenius" version instead of this one.

This MetaMod PHP recipe uses the new time and date constants in MetaMod 1.5h. If you haven’t upgraded yet, now’s the time!

Make sure you have set the Time Zone dropdown in MetaMod to your local time zone if you’re relying on time/date related functions.

Any of the following rules can be used independently of the others.
if ( MM_MONTH == 5 ) return XXX; /* month of May, any year */
/* 1st Feb 2009 to 24 May 2009 */
if ( MM_DATE >= 20090201 and MM_DATE <= 20090524) return XXX; 
/* 7:00-9:30 every Sunday */
if ( MM_TIME >= 70000 and MM_TIME < 93000 and MM_DAY_OF_WEEK == 0 ) return XXX;
/* every Saturday and Sunday */
if ( MM_DAY_OF_WEEK == 6 or MM_DAY_OF_WEEK == 0 ) return XXX; 
/* 6PM Friday to midnight Sunday night */
if ( ( MM_DAY_OF_WEEK == 5 and MM_TIME >= 180000 )
     or MM_DAY_OF_WEEK == 6
     or MM_DAY_OF_WEEK == 0 ) return XXX; 
if ( MM_DAY_OF_MONTH <= 7 ) return XXX; /* 1st to 7th day of every month */
// between 9-11AM, 4-6PM and 8-10PM Monday to Friday 
if (
 ( MM_DAY_OF_WEEK >= 1 and MM_DAY_OF_WEEK <= 5 ) and
  (
   ( MM_TIME > 90000 and MM_TIME < 110000 )
    or ( MM_TIME > 160000 and  MM_TIME < 180000 )
    or ( MM_TIME > 200000 and  MM_TIME < 220000 )
  )
 ) {
 return XXX;
}