我想要做的是在服务器上创建一个脚本,读取一个文本文件,对其进行排序,然后将其输出到一个javascript对象(可能通过JSON)。有问题的文本文件看起来是这样的:我应该如何在PHP中使用JavaScript存储日期?
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
处理filereading的PHP是这样的:
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;
}
}
然而,存储UNIX在开始和结束日期timesstamps。我会使用DateTime
类,但我目前仅限于PHP 4.4。理想情况下,我想存储在一个格式中的日期是:
- 可以比较的数字
- 是人类可读(允许
dates.txt
人编辑) - 一贯格式(即“01-01 -1900" 转换为‘01/01/1900’)
- 可以转换为JavaScript Date对象
我怎么会去存储日期,所以他们satify这些限制?
你忘由1000到乘法/除法 – 2009-09-14 18:52:17
你”再右吧。固定。 – 2009-09-15 00:01:40