【问题标题】:why am I getting '4:00' when I'm trying to display time using the date function with php?当我尝试使用 php 的日期函数显示时间时,为什么会得到“4:00”?
【发布时间】:2009-08-31 12:07:16
【问题描述】:

我正在尝试显示我在数据库中的时间。我设法让它以我需要的正确格式显示时间,但由于某种原因,它每次只显示“4:00”。

这是我的代码:

date('g:i', strtotime($row['startTime']))

我的数据库中显示的时间示例如下:00:12:30

为什么每次都显示“4:00”?

【问题讨论】:

  • 列的数据类型是什么?如果只回显 $row['startTime'] 会得到什么?
  • 等等...如果日期存储为00:12:30,并且您想显示小时(g)和分钟(i),为什么不简单地@987654323 @在哪里$dateText = $row['startTime'];?
  • 这是一个时间数据类型。当我简单地回显 $row['startTime'] 时,什么都没有出现,这很奇怪
  • 我能请你做两件事吗?首先:从“DESCRIBE tablename;”中添加(相关)详细信息其次:显示其中包含的数据样本。我很好奇为什么echo $row['startTime']; 什么都不做,并且想知道底层数据是否......与你预期的不同。
  • 有什么效果;问题解决了吗?有新的发展吗?

标签: php date time


【解决方案1】:

strtotime 需要一个日期时间格式......你应该这样做

date('g:i', strtotime('01 January 2009 ' . $row['startTime']))

【讨论】:

  • 检查 $row['startTime'] 是否有值,因为它听起来像是空的
【解决方案2】:

什么是底层数据库,startTime 列有什么数据类型?查看我拥有的最接近的 php 代码,strtoime 与 DB (MySQL) 中的 DATETIME 表示配合得很好。

【讨论】:

  • startTime 列是时间数据类型
【解决方案3】:

strtotime 将日期时间字符串转换为 Unix 时间戳。

也许您的 $row['startTime'] 不符合日期时间字符串的条件。

here 的示例都没有讨论不包含日期的日期时间字符串。

该链接还说如果strtotime被混淆,它会返回随机结果。我会再添加一些格式字符,看看还会返回什么。

【讨论】:

    【解决方案4】:

    如前所述,问题在于strtotime() 的使用。如果有任何用处,以下适用于我的机器:

     $date_text = $row['startTime']; // assuming the format "00:12:30"
     list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
     /* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
     these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
    see: http://us3.php.net/manual/en/function.explode.php
    and: http://us3.php.net/manual/en/function.list.php
     */
    
     echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";
    

    【讨论】:

    • 变量 $hrs、$mins 和 $secs 是从哪里来的?
    猜你喜欢
    • 2012-09-04
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2022-06-13
    相关资源
    最近更新 更多