$val)
{
if($f==$field)
{
$fpos = $i;
break;
}
$i++;
}
return $fpos;
}
/**
* splits the date into its parts
*
* @param string $date date in string form
* @param string $SourceFormat PHP Date formatting string
* @return Array Assoc. array containg date fields
*/
function GetDateArray($date,$SourceFormat)
{
$temp_format = GetStdFormat($SourceFormat);
$parts = preg_split("/\s*\D\s*/",$date);
$fpos = DateFieldOrder($temp_format,"year");
if($fpos>0)
$year = $parts[$fpos-1];
$fpos = DateFieldOrder($temp_format,"month");
if($fpos>0)
$month = $parts[$fpos-1];
$fpos = DateFieldOrder($temp_format,"day");
if($fpos>0)
$day = $parts[$fpos-1];
$date_parts = array();
$date_parts["year"] = $year;
$date_parts["month"] = $month;
$date_parts["day"] = $day;
return $date_parts;
}
/**
* convert date to Unix timestamp
*
* @param string $date date in string form
* @param string $SourceFormat PHP Date formatting string
* @return integer Unix Timestamp of date
*/
function DateTimestamp($date,$SourceFormat)
{
if(strlen($date))
{
$d = GetDateArray($date,$SourceFormat);
$ticks = adodb_mktime(0,0,0,$d["month"],$d["day"],$d["year"]);
}
else
$ticks = 0;
return $ticks;
}
function DateCurrTimeTimstamp($date, $SourceFormat)
{
if(strlen($date))
{
$d = GetDateArray($date,$SourceFormat);
$ticks = adodb_mktime(adodb_date("H"), adodb_date("i"), adodb_date("s"), $d["month"], $d["day"], $d["year"]);
}
else
$ticks = 0;
return $ticks;
}
/**
* convert date from one string format to another
*
* @param string $date date in string form
* @param string $SourceFormat PHP Date formatting string
* @param string $DestFormat PHP Date formatting string to return
* @return string reformatted date string
*/
function ReformatDate($date,$SourceFormat,$DestFormat)
{
$unixtime = DateTimestamp($date,$SourceFormat);
if($unixtime>0)
$newdate = adodb_date($DestFormat,$unixtime);
return $newdate;
}
/**
* Number of days in the month of a given date
*
* @param integer $date timestamp to check
* @return string Number of days in month
*/
function DaysInMonth($date)
{
return adodb_date("t",$date);
}
/**
* Checks a date for validity
*
* @param integer $date date to check
* @param string $format PHP Date formatting string
* @return boolean
*/
function ValidDate($date,$format)
{
$valid = TRUE;
$d = GetDateArray($date,$format);
$valid = $valid && ((int)$d["year"]>0);
$valid = $valid && ((int)$d["month"]>0);
if($valid)
{
$day = (int)$d["day"];
$checkdays = adodb_mktime(0,0,0,$d["month"],1,$d["year"]);
$days = DaysInMonth($checkdays);
$valid = $valid && (($day>0) && ($day<=$days));
}
return $valid;
}
function Is12HourTime($timestr)
{
$timestr = strtoupper($timestr);
return (strpos($timestr,"AM")>0 || strpos($timestr,"PM")>0);
}
function GetTimeParts($timestr)
{
if(Is12HourTime($timestr))
{
/* parse 12 hr clock */
list ($hour,$min,$sec,$ampm) = split('[: ]',$timestr);
if($ampm=="PM" && $hour != 12)
$hour = $hour + 12;
if($ampm=="AM" && $hour==12)
$hour=0;
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
}
else
$time = $timestr;
$parts = explode(":",$time);
return $parts;
}
function ValidTime($timestr)
{
global $objConfig;
$timestr = strtoupper($timestr);
$valid = is12HourMode() ? Is12HourTime($timestr) : !Is12HourTime($timestr);
$time = GetTimeParts($timestr);
$valid = $valid && (($time["hour"]<24) && ($time["min"]<60) && ($time["sec"]<60));
$valid = $valid && (($time["hour"]>-1) && ($time["min"]>-1) && ($time["sec"]>-1));
return $valid;
}
function SecondsSinceMidnight($timestr)
{
$timestr = strtoupper($timestr);
$time = GetTimeParts($timestr);
// echo $timestr."
\n";
// print_r($time);
$ticks = $time[2] + ($time[1]*60) + ($time[0]*3600);
return $ticks;
}
/*
$date = "10-03-71";
$source_format = "m-d-y";
print "date:". ReformatDate($date,$source_format,"m/d/Y")."
\n";
if(ValidDate($date,"Y.m.d"))
echo " Date is valid
\n";
*/
?>