【问题标题】:Little help with this "date" function这个“日期”功能帮助不大
【发布时间】:2010-02-13 12:21:39
【问题描述】:

我有这个从 mysql 数据库中选择一个日期,并将其与瑞典语言的月份名称数组进行比较。

$monthnames = array("","Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");

$postdate = $monthnames[date("n", strtotime( $row['modify_date'] ))];
//Outputs something like '12 Februari'

这里是prob,我想检查$postdate变量并根据日期将其更改为“今天”、“昨天”和“前天”,我该怎么做?

谢谢

【问题讨论】:

    标签: php sql mysql html datetime


    【解决方案1】:

    如果您使用时间戳在数据库中存储日期:

    您可以有如下预设间隔:

    $day = mktime(0,0,0,2,1,2001)-mktime(0,0,0,1,1,2001);
    

    您可以将现在的时间 - 您在数据库中的时间戳/日期与这些时间间隔进行比较。

    例如,假设 $dbTime 包含使用 mysql 获取的时间戳:

    $time = time() - $dbTime;
    if($time<$day)
       echo 'posted today';
    elseif($time<($day*2))
       echo 'posted yesterday';
    

    等等等等

    然后,您可以使用 PHP 的 Date 函数来回显小时、分钟、秒或日期,如果它们大于您预定义的间隔。

    【讨论】:

      【解决方案2】:

      PHP 中没有这样的内置函数。您必须进行计算,在这种情况下,mktime 函数可以成为您的朋友。

      if (date of $timenow == date of $timepost) { today; }
      else if (date of $timenow - 24h == date of $timepost) { yesterday; }
      etc.
      

      注意夏季/冬季时间变化、时区等,因此请尝试使用mktime()

      http://php.net/date

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        • 1970-01-01
        相关资源
        最近更新 更多