Skip to content

Instantly share code, notes, and snippets.

@Asenar
Created May 4, 2012 10:22
Show Gist options
  • Save Asenar/2593862 to your computer and use it in GitHub Desktop.
Save Asenar/2593862 to your computer and use it in GitHub Desktop.
<?php
private function getCurrentRule() {
$return = false;
$rules = array_reverse($this->getRules());
$today = time() ;
$year = Date('Y') ;
$day = Date('d') ;
$month = Date('m') ;
$hour = Date('H') ;
$minute = Date('i') ;
/*$today = mktime(7, 1, 0, 04, 26, 2010);
$year = Date('Y', $today) ;
$day = Date('d', $today) ;
$month = Date('m', $today) ;
$hour = Date('H', $today) ;
$minute = Date('i', $today) ;
var_dump(date('r', $today), '<br/>');*/
foreach ($rules as $rule) {
if ($rule['active']) {
list($date, $time) = explode(' ', $rule['date_start']);
list($start['year'], $start['month'], $start['day']) = explode('-', $date);
list($start['hour'], $start['minute'], $start['second']) = explode(':', $time);
$start['year'] = $year;
$start['month'] = $start['month']!='00'?$start['month']:$month;
if ($start['day'] == '00') {
$start['day'] = $day;
if (60*$start['hour'] + $start['minute'] > 60*$hour + $minute)
$start['day']--;
}
list($date, $time) = explode(' ', $rule['date_end']);
list($end['year'], $end['month'], $end['day']) = explode('-', $date);
list($end['hour'], $end['minute'], $end['second']) = explode(':', $time);
// deal with end time < start time on same day
if ($end['day'] == '00') {
$end['day'] = $day;
if ($start['day'] == $end['day']
&& (60*$end['hour'] + $end['minute']) < (60*$start['hour'] + $start['minute']))
$end['day']++ ;
}
// end day < start day on same month
if ($end['month'] == '00') {
$end['month'] = $month;
if ($start['month'] == $month
&& (24*60*$end['day'] + 60*$end['hour'] + $end['minute']) < (24*60*$start['day'] + 60*$start['hour'] + $start['minute']))
$end['month']++ ;
}
// end month < start month on same year
$end['year'] = $year;
if ($end['month'] < $start['month'])
$end['year']++;
if ($start['month'] > $month) {
$start['year']--;
$end['year']--;
}
$time_start = mktime($start['hour'], $start['minute'], 0, $start['month'], $start['day'], $start['year']) ;
$time_end = mktime($end['hour'], $end['minute'], 59, $end['month'], $end['day'], $end['year']) ;
//var_dump($rule['name'], date('r', $time_start), date('r', $time_end), '<br/>');
if ($today >= $time_start && $today <= $time_end)
$return = $rule;
}
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment