【问题标题】:codeigniter get timestamp from database and formatcodeigniter 从数据库和格式中获取时间戳
【发布时间】:2011-10-29 02:48:12
【问题描述】:

我有一个timestamp 类型的MYSQL 表列,并在添加新记录时默认使用CURRENT_TIMESTAMP

我的数据库表中的日期字段如下所示2011-08-16 12:09:25 如何将其格式化为MM/DD/YY 以显示在我的网站上?我尝试了date helper 的一些功能,但出现错误。

注意:如果可能的话,我正试图弄清楚如何为此使用 Codeigniter 函数。

在 CI 手册中有 $timestamp = '1140153693'; 的示例,但我的数据库时间戳是不同的格式并出现错误。

【问题讨论】:

  • 你得到了什么错误,你的代码是什么样的?
  • @Cyber​​Junkie:请不要只说“我有错误”。告诉我们错误是什么。

标签: php mysql codeigniter date


【解决方案1】:

要显示时间戳,只需使用 PHP Date 类。但是由于 MySQL 将时间戳输出为字符串而不是 INT,因此您必须首先使用 PHP 的 strtotime 函数将时间戳字符串转换为时间戳 INT。代码如下所示:

echo date("m/d/y",strtotime($timestamp));

日期 http://www.php.net/manual/en/function.date.php

StrToTime http://php.net/manual/en/function.strtotime.php

【讨论】:

  • 谢谢!我刚刚想通了。我是 php 中的日期和时间新手。
  • 您的方法实际上似乎比我使用的 Codeigniter 辅助函数更简单
【解决方案2】:

对不起,我没有正确解释这个问题,但已经解决了。

为了使用 Codeigniter 日期函数,我必须将 mysql ISO 8601 日期格式转换为 32 位整数。

我必须做以下事情

$unix = mysql_to_unix($row->message_date); //conversion to string

$datestring = "%m/%d/%Y";

echo mdate($datestring, $unix);

我使用的是 ISO 8601 格式并出现错误。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多