What I want to do is make a script on the server read a text file, sort it, then output it to a javascript object (probably via JSON). The text file in question looks something like this:
13/09/2009,17/09/2009,Arbitrary dates 14/09/2009,18/09/2009,Some random comment 14/09/2010,18/12/2010,A comment to the dates 14/09/2010,18/09/2010,A subset of another date 14/09/2001,18/09/2002,The oldest date
The php to handle the filereading looks like this:
function loadDates()
{
$dateFile = fopen("dates.txt", "rt");
$dates = array();
if($dateFile)
{
flock($dateFile,LOCK_SH);
$i = 0;
while(!feof($dateFile))
{
$text = fgets($dateFile);
if($text !== FALSE)
{
$i++;
$arr = explode(",",$text,3);
//actual storage
$dates[$i]['start'] = strtotime($arr[0]);
$dates[$i]['end'] = strtotime($arr[1]);
$dates[$i]['comment'] = $arr[2];
}
}
fclose($dateFile);
//sort by start date, then by end date
foreach($dates as $key => $item)
{
$start[$key] = $item['start'];
$end[$key] = $item['end'];
}
array_multisort($start, SORT_ASC, $end, SORT_ASC, $dates);
return $dates;
}
else
{
return FALSE;
}
}
However, that stores unix timesstamps in the start and end dates. I would use the DateTime
class, but I'm currently restricted to PHP 4.4. Ideally, I'd like to store the dates in a format that:
- Can be compared numerically
- Are human readable (allowing human editing of
dates.txt
) - Are consistently formatted (ie "01-01-1900" is converted to "01/01/1900")
- Can be converted to a javascript Date object
How would I go about storing the dates so they satify these restrictions?