So far, I've allowed the programmer to use two different approaches to my class(es). They can allow the class to determine the data type of the sorting column, or they can specify one. The auto-sort works well, with the exception of some items it thinks are dates, but actually aren't. My question here is twofold: 1. - Is it worth solving to allow a programmer to stuff things into my class and it works like *magic*, 2. - What could I do to better determine if a scalar is, indeed, a date?
Below is the code for my logical structure as well as the IsDate Helper.
The chunk that decides what type we're working with
Code: Select all
/**
* If we're running an auto sort, figure out what data type we're
* working with, commit the sort.
*/
if( self::$sort_type == self::SORT_AUTO )
{
if(is_numeric($data[0][self::$sortby_key]))
usort($data,'SimpleSort::Cmp');
elseif(is_float($data[0][self::$sortby_key]))
usort($data,'SimpleSort::Cmp');
elseif(self::IsDate($data[0][self::$sortby_key]))
usort($data,'SimpleSort::DateCmp');
else
usort($data,'SimpleSort::StringCmp');
}Code: Select all
/**
* Helper method for SimpleSort::Sort(). This function evaluates and
* breaks down a string to determine if it's a valid date. Buggy.
*
* TODO: provide bug examples.
*/
private static function IsDate($string)
{
$t = strtotime($string);
$m = date('m',$t);
$d = date('d',$t);
$y = date('Y',$t);
return checkdate ($m, $d, $y);
}-Andy