【问题标题】:Displaying strtotime() effectively in MySQL/PHP query在 MySQL/PHP 查询中有效显示 strtotime()
【发布时间】:2011-10-06 12:18:43
【问题描述】:

这是我的 PHP/MySQL 查询,如 Displaying links in PHP/MySQL? 所述:

http://pastebin.com/5Td9Bybn

我承认,我忘记了如何有效地使用 strtotime() 函数,因为我想以这种格式显示我的演出时间:

06:00

10:00

(末尾没有 :00,例如 10:00:00)

代码现在显示此错误:

解析错误:语法错误,第 11 行 C:\www\vhosts\localradio\schedsun.php 中出现意外 T_ECHO

我已经修复了上一个问题中的错误,如果我想将时间显示为:

早上 6:00

06:00

早上 6 点

(仅供测试,在Pastebin上的代码参数内)。

我基本上只是问,因为我在 PHP 的其他部分上自己的进修课程,花了这么长时间学习数组等。

最后,我是否需要作为变量 - 还是不需要 $result_ar?

感谢您对最后一个问题的帮助!

【问题讨论】:

    标签: php mysql arrays datetime parse-error


    【解决方案1】:

    选项 1

    strtotime() 会给你一个时间戳,你使用函数date() 来格式化时间和/或日期的显示。

    06:00

    <?php echo date('H:i', strtotime($result_ar['airtime'])); ?>
    

    上午 6:00

    <?php echo date('g:ia', strtotime($result_ar['airtime'])); ?>
    

    早上 6 点

    <?php echo date('ga', strtotime($result_ar['airtime'])); ?>
    

    了解date() and its formatting on php.net


    选项 2

    按照@Marc B 的建议,在 MySQL 中执行。

    改变这一行

    $result = mysql_query("SELECT * FROM presenters1;", $connection) or die("error querying database");
    

    $result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%h:%i') `airtime` FROM presenters1;", $connection) or die("error querying database");
    

    然后,改变这一行:

    <div class="time"><? php echo strotime ('H') $result_ar['airtime']; ?></div>
    

    <div class="time"><?php echo $result_ar['airtime']; ?></div>
    

    【讨论】:

      【解决方案2】:

      PHP函数strtotime()的用法如下:

      int strtotime ( string $time [, int $now ] )
      

      这意味着您传入时间的字符串值,以及可选的当前时间值,即 UNIX 时间戳。返回的值是一个整数,它是一个 UNIX 时间戳。

      这种用法的一个示例如下,其中传递给 strtotime() 的日期可能是来自数据库查询或类似的日期:

      $ts = strtotime('2007-12-21');
      

      这会将值 1198148400 返回到 $ts 变量中,这是 2007 年 12 月 21 日的 UNIX 时间戳。这可以使用 date() 函数来确认,如下所示:

      echo date('Y-m-d', 1198148400);
      // echos 2007-12-21
      

      strtotime() 能够解析各种字符串并将它们转换为适当的时间戳,使用实际日期以及诸如“下周”、“下周二”、“上周四”、“2 周前”等字符串“ 等等。以下是一些示例:

      $ts = strtotime('21 december 2007');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      

      这将显示以下内容:

      1198148400 2007-12-21

      如果今天是 12 月 21 日,则如下:

      $ts = strtotime('next week');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      
      $ts = strtotime('next tuesday');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      
      $ts = strtotime('last thursday');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      
      $ts = strtotime('2 weeks ago');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      
      $ts = strtotime('+ 1 month');
      echo $ts, '<br />';
      echo date('Y-m-d', $ts), '<br />';
      

      将显示以下内容:

      1199006542
      2007-12-30
      1198494000
      2007-12-25
      1198062000
      2007-12-20
      1197192142
      2007-12-09
      1201080142
      2008-01-23
      

      【讨论】:

        【解决方案3】:

        &lt;? 和单词 php 之间有一个空格。我认为这导致了意外的回声错误。你应该有&lt;?php

        【讨论】:

          【解决方案4】:

          directly in MySQL 可以做到,这样可以节省 strtotime() 的解析开销,并保证产生正确的结果,因为 strtotime 偶尔会猜错。

          SELECT DATE_FORMAT(yourdatefield, '%h:%i') ...
          

          【讨论】:

          • 这是怎么做的: $result = mysql_query("SELECT DATE_FORMAT (airtime, %H) * FROM presenters1 ;", $connection) or die("error querying database");跨度>
          • 你需要在格式字符串周围加上引号,所以'%H',并将你的 die() 调用更改为 die(mysql_error()),它会告诉你查询失败的确切原因。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-14
          • 2012-10-03
          • 1970-01-01
          相关资源
          最近更新 更多