我应该如何在 PHP 中存储日期以便与 javascript 一起使用?
我想做的是让服务器上的脚本读取文本文件,对其进行排序,然后将其输出到 javascript 对象(可能通过 JSON)。有问题的文本文件看起来像这样:
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
处理文件读取的 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 时间戳。我会使用 DateTime
类,但目前仅限于 PHP 4.4。理想情况下,我希望以以下格式存储日期:
- 可以进行数字
- 比较 人类可读(允许人工编辑
dates.txt
) - 格式一致(即“01-01-1900”转换为“01/01/1900”)
- 可以转换为 javascript Date 对象
我将如何存储日期以便它们满足这些限制?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用UNIX时间戳
最安全的是在javascript中
,您可以在php中使用日期函数将时间戳作为第二个参数。
请参阅 http://jp.php.net/manual/en/function.date .php
和 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects /日期
编辑:
另请参阅 strftime http://jp.php.net/manual/en/function .strftime.php
编辑:
注意:javascript 函数需要毫秒,而 php 函数需要秒。将 javascript 的输出除以 1000 或使用如下所示的内容:
The safest is to use UNIX timestamps
in javascript, you can use
in php the date function takes the timestamp as second parameter.
see http://jp.php.net/manual/en/function.date.php
and https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date
EDIT:
Also see strftime http://jp.php.net/manual/en/function.strftime.php
EDIT:
Note: the javascript function takes milliseconds, and the php functions use seconds. divide the output of the javascript by 1000 or use something like the following:
这样存储日期:
19991231 = Dec. 31, 1999
20000704 = July 4, 2000
人类可读,绝对可排序,并且您可以创建一个 JavaScript 函数来进行转换。
我将为您提供一个来自我疯狂头脑的黑客:(
假设 x 是 yyyymmdd 形式的日期)
Store the dates thus:
19991231 = Dec. 31, 1999
20000704 = July 4, 2000
Human readable, definitely sortable, and you can make a JavaScript function for the conversion.
I will provide you with a hack from my deranged mind:
(this assumes that x is that date in yyyymmdd form)