<?php
/**
* simbio_date class
* A Collection of static function for doing date arithmatic related operation
*
* Copyright (C) 2007,2008 Arie Nugraha (hide@address.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
class simbio_date
{
/* ALL METHODS DATE ARGUMENT(s) IS ASSUMED USING YYYY-MM-DD format */
/**
* Static Method to get next date
*
* @param integer $int_day_num
* @param string $str_start_date
* @param string $str_start_format
* @return string
*/
public static function getNextDate($int_day_num = 1, $str_start_date = '', $str_date_format = 'Y-m-d')
{
if ($int_day_num < 1) {
return $str_start_date;
}
if (!$str_start_date) {
return date($str_date_format, mktime(0, 0, 0, intval(date('n')), (intval(date('j'))+$int_day_num), intval(date('Y')) ) );
} else if (preg_match('/.+\-.+\-.+/i', $str_start_date)) {
list($_year, $_month, $_daym) = explode('-', $str_start_date);
return date($str_date_format, mktime(0, 0, 0, intval($_month), (intval($_daym)+$int_day_num), intval($_year) ) );
} else {
return null;
}
}
/**
* Static Method to get previous date
*
* @param integer $int_day_num
* @param string $str_start_date
* @param string $str_date_format
* @return string
*/
public static function getPrevDate($int_day_num = 1, $str_start_date = '', $str_date_format = 'Y-m-d')
{
if ($int_day_num < 1) {
return $str_start_date;
}
if (!$str_start_date) {
return date($str_date_format, mktime(0, 0, 0, intval(date('n')), (intval(date('j'))-$int_day_num), intval(date('Y')) ) );
} else if (preg_match('/.+\-.+\-.+/i', $str_start_date)) {
list($_year, $_month, $_daym) = explode('-', $str_start_date);
return date($str_date_format, mktime(0, 0, 0, intval($_month), (intval($_daym)-$int_day_num), intval($_year) ) );
} else {
return null;
}
}
/**
* Static Method to get number of day between dates
*
* @param string $str_start_date
* @param string $str_end_date
* @return integer
*/
public static function calcDay($str_start_date, $str_end_date)
{
list($_startYear, $_startMonth, $_startDaym) = explode("-", $str_start_date);
list($_endYear, $_endMonth, $_endDaym) = explode("-", $str_end_date);
$_startMKtime = mktime(0, 0, 0, $_startMonth, $_startDaym, $_startYear);
$_endMKtime = mktime(0, 0, 0, $_endMonth, $_endDaym, $_endYear);
$_mksec = $_endMKtime-$_startMKtime;
return abs(intval($_mksec/(3600*24)));
}
/**
* Static Method to get number of holiday between dates
*
* @param string $str_start_date
* @param string $str_end_date
* @param array $array_holiday_name
* @param array $array_holiday_date
* @return integer
*/
public static function countHolidayBetween($str_start_date, $str_end_date, $array_holiday_dayname = array('Sun'), $array_holiday_date = array())
{
$_holiday_count = 0;
$_one_day = 3600*24;
list($_startYear, $_startMonth, $_startDaym) = explode("-", $str_start_date);
list($_endYear, $_endMonth, $_endDaym) = explode("-", $str_end_date);
$_startMKtime = mktime(0, 0, 0, $_startMonth, $_startDaym, $_startYear);
$_endMKtime = mktime(0, 0, 0, $_endMonth, $_endDaym, $_endYear);
while ($_startMKtime <= $_endMKtime) {
if (in_array(date('D', $_startMKtime), $array_holiday_dayname) OR in_array(date('Y-m-d', $_startMKtime), $array_holiday_date)) {
$_holiday_count += 1;
}
$_startMKtime += $_one_day;
}
return $_holiday_count;
}
/**
* Static Method to compare dates and return the latest date
*
* @param string $str_date_to_compares
* @return string
*/
public static function compareDates()
{
if (func_num_args() < 1) {
return null;
} else if (func_num_args() == 2) {
// get value of method arguments
$date1 = func_get_arg(0);
$date2 = func_get_arg(1);
// check fi $date1 and $date2 is same
if ($date1 == $date2) {
return null;
}
// get the UNIX timestamp of date
list($_year1, $_month1, $_daym1) = explode('-', $date1);
list($_year2, $_month2, $_daym2) = explode('-', $date2);
$timestamp1 = mktime(0, 0, 0, $_month1, $_daym1, $_year1);
$timestamp2 = mktime(0, 0, 0, $_month2, $_daym2, $_year2);
if ($timestamp1 > $timestamp2) {
return $date1;
} else {
return $date2;
}
}
$func_args = func_get_args();
$latest = func_get_arg(0);
foreach ($func_args as $args) {
$latest = self::compareDates($latest, $args);
}
return $latest;
}
/**
* Static Method to get next date that are not holidays
*
* @param string $str_date
* @param array $array_holiday_dayname
* @param array $array_holiday_date
* @return string
*/
public static function getNextDateNotHoliday($str_date, $array_holiday_dayname = array(), $array_holiday_date = array())
{
// if array dayname and date is empty
if (!$array_holiday_dayname AND !$array_holiday_date) {
return $str_date;
}
// parse date
list($_year, $_month, $_daym) = explode('-', $str_date);
// get dayname of $str_date
$dayname = date('D', mktime(0, 0, 0, $_month, $_daym, $_year));
// check date array first
if ($array_holiday_date) {
$d = false;
foreach ($array_holiday_date as $_idx=>$_each_date) {
if (substr($str_date, -5) == substr($_each_date, -5)) {
$d = true;
unset($array_holiday_date[$_idx]);
}
}
if ($d) {
$_str_date_next = self::getNextDate(1, $str_date);
return self::getNextDateNotHoliday($_str_date_next, $array_holiday_dayname, $array_holiday_date);
} else {
// check dayname
if (!in_array($dayname, $array_holiday_dayname)) {
return $str_date;
} else {
$_str_date_next = self::getNextDate(1, $str_date);
return self::getNextDateNotHoliday($_str_date_next, $array_holiday_dayname, $array_holiday_date);
}
}
} else {
// check dayname
if (!in_array($dayname, $array_holiday_dayname)) {
return $str_date;
} else {
$_str_date_next = self::getNextDate(1, $str_date);
return self::getNextDateNotHoliday($_str_date_next, $array_holiday_dayname);
}
}
}
}
?>