class ModelExtensionModuleTimer extends Model {
public function checkExistenceExtension($type, $extension_name) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "' AND `code` = '" . $this->db->escape($extension_name) . "' ");
return $query->rows;
}
public function getFullDateTime($date_end, $weekdays = '', $hours = '') {
$old_date_end = $date_end;
$newdate = explode('-', $date_end);
$new_date_end = '';
$all_weekdays = '0,1,2,3,4,5,6';
$all_hours = '00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23';
// Looping
if ($newdate[0] == 0000) $newdate[0] = date("Y");
if ($newdate[1] == 00) $newdate[1] = date("m");
if ($newdate[2] == 00) $newdate[2] = date("d");
// To prevent a date error
if($newdate[2] != date("d"))
$newdate[2] = $newdate[2]-1;
$date_end = implode("-", $newdate);
// All days and hours, then return date end
if (!$weekdays || !$hours || ($weekdays == $all_weekdays && $hours == $all_hours)) {
$new_date_end = $date_end . " 23:59:59";
// The case when selected all hours, but not all weekdays
} elseif ($weekdays && $weekdays != $all_weekdays) {
$weekday = date('w');
$days_offset = $this->getWeekdayOffset($weekday, $weekdays);
$new_date_end = date('Y-m-d', strtotime('+' . $days_offset . ' day')) . ' 23:59:59';
}
// Additional check for hours
if ($hours && $hours != $all_hours) {
$hour = date('H', strtotime('+1 hour'));
$hours_offset = 0;
// if ($date_end == date('Y-m-d'))
// $hour = '00';
$plus_hours = false;
// If the hour is not found, go out from the loop
while (strpos($hours, (string) $hour) !== false) {
$hour++;
$hours_offset++;
if ($hour == 24) {
$hour = '00';
}
if ($hour == 1) {
$plus_hours = true;
}
if ($hour < 10 && $hour != '00') {
$hour = '0' . $hour;
}
}
$hour--;
if ($hour < 0)
$hour = 23;
if ($hour < 10)
$hour = '0' . $hour;
if ($plus_hours) {
$days_offset = 1; // + 1 day
$weekday = date("w", strtotime('+1 day'));
// If the weekday is found, then go out from the loop
while (strpos($weekdays, (string) $weekday) === false) { // Not found
$weekday++;
$days_offset++;
if($weekday == 7) {
$weekday = 0;
}
}
$new_date_end = date('Y-m-d ', strtotime('+' . $days_offset . ' day')) . $hour . ':59:59';
} else {
$new_date_end = date('Y-m-d ') . $hour . ':59:59';
}
}
// Checking on the new date end and old date end exceeding
if ($old_date_end != '0000-00-00' && strtotime($date_end . ' 23:59:59') < strtotime($new_date_end)) {
return $old_date_end;
} else {
return $new_date_end;
}
}
public function getWeekdayOffset($weekday, $weekdays) {
$days_offset = 0;
// If the weekday is not found, then go out from the loop
while (strpos($weekdays, (string) $weekday) !== false) {
$weekday++;
$days_offset++;
if($weekday == 7) {
$weekday = 0;
}
}
if ($days_offset != 0)
$days_offset--;
return $days_offset;
}
public function getSpecialDateDiff($special_datetime_end) {
if ($special_datetime_end == '0000-00-00') {
$special_datetime_end = $this->getFullDateTime($special_datetime_end);
}
$special_datetime_end = strtotime($special_datetime_end);
// calculate the difference between dates
$special_date_diff = $special_datetime_end - time();
return $special_date_diff;
}