【问题标题】:How to get current localtime using Mysql and PHP?如何使用 Mysql 和 PHP 获取当前本地时间?
【发布时间】:2009-10-09 07:45:21
【问题描述】:

我从mysql网站上读到,我执行这个:

SELECT CURTIME()

但是如何获得价值呢?使用 mysql_fetch_assoc()??

【问题讨论】:

    标签: php mysql time


    【解决方案1】:

    是的,您将其作为普通查询执行。您可能希望将列别名为更好的名称:

    $data = mysql_query('SELECT CURTIME() AS the_time');
    $row = mysql_fetch_assoc($data);
    $theTime = $row['the_time'];
    

    通过 PHP 获得时间比执行昂贵的 SQL 查询更有效。

    你可以使用date()函数:

    $theTime = date('H:i:s');
    

    【讨论】:

      【解决方案2】:
      SELECT CURTIME() as time_now
      

      *编辑

      $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
      $sql = "SELECT CURTIME() as time_now";
      $result = mysql_query($sql);
      while ($row = mysql_fetch_assoc($result)) {
          echo $row["time_now"];
      }
      

      【讨论】:

      • 你在使用类吗?如果不检查 greg 的答案
      猜你喜欢
      • 2021-11-27
      • 2014-07-06
      • 2011-04-27
      • 1970-01-01
      • 2021-09-26
      • 2015-08-16
      • 2018-03-04
      • 2013-10-15
      • 2018-04-10
      相关资源
      最近更新 更多