Zend - The PHP Company




Date & Time

Add Code


date_in_interval()  

Type: code fragment
Added by: darkelder
Entered: 30/05/2002
Last modified: 06/12/2001
Rating: - (fewer than 3 votes)
Views: 6185
Check if the time is in some intervals.


<?php


/**
  * Function date_in_interval check if the time have a valid interval
  *
  * I use this to check intervals users can log-in on chat.
  * The format of interval is the two-dimensional array:
  * array(array interval, array interval), where interval is
  * an array, witch first element is the week day (sunday is 0, monday is 1, ...),
  * and the second elemnet is the start valid time, and the third element is
  * the third valid time.
  * The time format is "hh[:mm[:ss]]", you need specify the hour (hh), the
  * minute (mm) and second (ss) is optional. 
  * Third element is optional and set end of time interval. If not specified
  * Will be "23:59:59". 
  *
  * Hour is in 24 hour format.
  * 
  * Generated PHPDoc at http://opensource.under.com.br/date_in_interval/docs/
  *
  * @package        date_in_interval
  * @author        Roberto Bert�, darkelder (inside) users (dot) sourceforge (dot) net
  * @version        1.0rc1
  * @since        2002-05-29
  * @copyright        no copyright, LGPL - write to my email if you want receive the license or check-in www.gnu.org
  * @return        bool            return TRUE if date is in interval, or FALSE if not
  * @param        mixed            $intervals    the mixed array described above
  * @param        int            $time        (optional)    if not set, use time()
  * @param        bool            $debug         (optional)    show
  */
function date_in_interval($intervals,$time 0,$debug FALSE)
{
    
// time default get, integer
    
if ($time == 0$time time();
    
settype($time,"integer");
    
    
// week day
    
$tDay date("w",$time);
    
// hour 
    
$tHou date("G",$time);
    
// minute
    
$tMin date("i",$time);
    
// seconds
    
$tSec date("s",$time);
    
    
    
// to each interval on intervals
    
foreach ($intervals as $interval)
    {
        
        list(
$iDay,$ia,$ib) = $interval;
        list(
$iaHou,$iaMin,$iaSec) = explode(":",$ia);
        
        
// the default of third element is 23:59:59
        
if (!isset($ib))
        {
            
$ib "23:59:59";
        }
        list(
$ibHou,$ibMin,$ibSec) = explode(":",$ib);

        
// everything should be integer
        
settype($iDay,"integer");
        
settype($iaHou,"integer");
        
settype($iaMin,"integer");
        
settype($iaSec,"integer");
        
settype($ibHou,"integer");
        
settype($ibMin,"integer");
        
settype($ibSec,"integer");
        
        
        
// debug
        
if ($debug == TRUE)
        {
            
$lastdebug sprintf("%st%st%st%st%st%st%s",$iDay,$iaHou,$iaMin,$iaSec,$ibHou,$ibMin,$ibSec);
        }
    
        
// creating interval times
        
$tMulti  $tHou  3600 $tMin  60 $tSec;
        
$iaMulti $iaHou 3600 $iaMin 60 $iaSec;
        
$ibMulti $ibHou 3600 $ibMin 60 $ibSec;
        
        if (
$tDay == $iDay && $iaMulti <= $tMulti && $ibMulti >= $tMulti)
        {
            
// debug
            
if ($debug == TRUE
            {    
                
printf("%s = <b>valid</b><br>",$lastdebug);
            }

            return 
TRUE;
        }
        elseif (
$debug == TRUE) {
            
printf("%s = <b>invalid</b><br>",$lastdebug);
        }
    }
    
// default is return false
    
return FALSE;
}




?>

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories