duanlinpi0265 2014-07-25 17:35
浏览 73
已采纳

使用PHP,CodeIgniter和MySQL显示查询日期字段问题?

I altered a table in MySQL to be a timestamp so I could see when a user was created:

ALTER TABLE  `users` CHANGE  `created_date`  `created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;

Using CodeIgniter I queried the database and got my row results for all users:

$this->db->get('users');

In my view I'm trying to display the results using PHP date():

<?php echo date("d-m-Y", $row->created_date); ?>

and I'm getting this error message, which I don't understand:

Severity: Notice
Message: A non well formed numeric value encountered
Filename: users/view_all_users.php
Line Number: 19

where by itself:

<?php echo $row->created_date ?>

produces:

01-01-1970 0000-00-00 00:00:00

Any ideas what I've done wrong?

  • 写回答

2条回答 默认 最新

  • dongzhuo8210 2014-07-25 17:40
    关注

    the second argument for date() must be a valid time stamp. 01-01-1970 0000-00-00 00:00:00 is not a valid timestamp.

    $arr = explode(' ',$row->created_date);
    $date = $arr[1] . ' ' . $arr[2];
    echo date("d-m-Y", strtotime($date));
    

    note that this string: 01-01-1970 0000-00-00 00:00:00 probably denotes that the value in your field is wrong, but this string: 1970-01-01 00:00:00 is valid for use with strtotime()

    edit I have an eerie feeling though, are you sure that the full string is outputed by the second echo, or are your two echos taking place, hence mimicking an invalid string? in which case this:

    echo date("d-m-Y", strtotime($row->created_date));
    

    will work right away!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?