php CodeIgniter:格式化mysql DATETIME字段dd/mm/yy hh:mm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:45:52  来源:igfitidea点击:

CodeIgniter: format mysql DATETIME field dd/mm/yy hh:mm

phpdatetimecodeigniterdate-format

提问by iamjonesy

Hi pretty much what it says on the tin.

嗨,罐头上写的差不多。

I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.

我有一个日期时间 mysql 字段,我想以 dd/mm/yyyy hh:mm 格式输出,例如 07/01/2011 22:16。

I've tried:

我试过了:

<?php 
  $datestring = '%d/%m/%Y %h:%i';  
  echo mdate($datestring,$row->created); 
?>

But I'm getting an error:

但我收到一个错误:

Message: A non well formed numeric value encountered

Any help most appreciated!

任何帮助最感谢!

Cheers,

干杯,

Billy

比利

回答by profitphp

Try:

尝试:

echo date ("d/m/Y h:ia",strtotime($row->created));

回答by treeface

The second parameter of the mdate()function still needs to be an integer timestamp, just like the native PHP date()function. Try using the strtodate()function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:

mdate()函数的第二个参数仍然需要是一个整数时间戳,就像原生的PHPdate()函数一样。尝试使用strtodate()接受字符串作为参数(包括 MySQL 日期格式)并返回整数的函数。这可以像这样完成:

$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));

The only difference between mdate()and date()is, as the CodeIgniter docs say:

mdate()和之间的唯一区别date()是,正如 CodeIgniter 文档所说:

This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.

The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.

此函数与 PHP 的 date() 函数相同,不同之处在于它允许您使用 MySQL 样式的日期代码,其中每个代码字母前面都有一个百分号:%Y %m %d 等。

以这种方式处理日期的好处是您不必担心转义任何不是日期代码的字符,因为您通常必须使用 date() 函数。

回答by al_manchester

Got this to work using treeface's solution, with one minor change:

使用 treeface 的解决方案让这个工作,有一个小改动:

$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created)); 
    //strtoDATE didn't work but strtoTIME did

Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.

让我挠了几个小时,但现在它可以工作了,我可以继续使用 CI 助手来处理所有日期函数。

HTH

HTH

回答by Pakiza

I'm using:

我正在使用:

mdate(date_string,mysql_to_unix($row->created))

That should work.

那应该工作。