我应该如何在 PHP 中存储日期以便与 javascript 一起使用?

发布于 2024-08-05 10:10:21 字数 1583 浏览 4 评论 0原文

我想做的是让服务器上的脚本读取文本文件,对其进行排序,然后将其输出到 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。理想情况下,我希望以以下格式存储日期:

  1. 可以进行数字
  2. 比较 人类可读(允许人工编辑 dates.txt
  3. 格式一致(即“01-01-1900”转换为“01/01/1900”)
  4. 可以转换为 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:

  1. Can be compared numerically
  2. Are human readable (allowing human editing of dates.txt)
  3. Are consistently formatted (ie "01-01-1900" is converted to "01/01/1900")
  4. Can be converted to a javascript Date object

How would I go about storing the dates so they satify these restrictions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-08-12 10:10:21

使用UNIX时间戳

最安全的是在javascript中

var mydate = new Date();
mydate.getTime(); //timestamp
mydate.setTime(your_timestamp); //set using timestamp

,您可以在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 或使用如下所示的内容:

Date.prototype.getTimeInSeconds = function() {
    return this.getTime()/1000;
}

var mydate = new Date();
mydate.getTimeInSeconds(); //PHP-compatible timestamp

The safest is to use UNIX timestamps

in javascript, you can use

var mydate = new Date();
mydate.getTime(); //timestamp
mydate.setTime(your_timestamp); //set using timestamp

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:

Date.prototype.getTimeInSeconds = function() {
    return this.getTime()/1000;
}

var mydate = new Date();
mydate.getTimeInSeconds(); //PHP-compatible timestamp
好听的两个字的网名 2024-08-12 10:10:21

这样存储日期:

19991231 = Dec. 31, 1999

20000704 = July 4, 2000

人类可读,绝对可排序,并且您可以创建一个 JavaScript 函数来进行转换。

我将为您提供一个来自我疯狂头脑的黑客:(

假设 x 是 yyyymmdd 形式的日期)

new Date((x-(x%10000))%9999,(((x%10000)-(x%100))%99)-1,x%100)

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)

new Date((x-(x%10000))%9999,(((x%10000)-(x%100))%99)-1,x%100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文