我使用CI作为我的框架。我怎样才能设置一个CONSTANT日期格式,这样我就不需要在所有文件中更改和搜索date("y-m-d")
?
我还没有尝试过你所要求的,但是在某个地方你应该能够使用define
函数来定义一个匹配你的格式的常量字符串,然后你可以在整个应用程序中引用。
的例子:
define( 'MY_DATE_FORMAT', "y-m-d" );
$date = date( MY_DATE_FORMAT );
在CodeIgniter中放置这个是完全不同的问题。我要看一下文件,看看能找到什么。
HTH .
编辑:在CI网站上找到这个论坛主题:http://codeigniter.com/forums/viewthread/185794/它应该让你开始做你需要做的事情。
放这是一个通用头文件:
define('my_date_format', 'y-m-d');
使用常量:
// remember to include the header file first
date(my_date_format);
将2010-11-10 10:12:11
格式化为date(m.d.y)
:
$myDate = new DateTime('2010-11-10 10:12:11');
$myDate->format('m.d.y');
我认为当您说"常量"日期格式时,您的意思是希望每次都有相同的输出,但您实际上并不需要PHP意义上的常量。只需编写您自己的函数(或者用Codeigniter术语来说,"helper"):
// Apply your default format to $format
function display_date($timestamp = NULL, $format = 'y-m-d')
{
// Possibly do some stuff here, like strtotime() conversion
if (is_string($timestamp)) $timestamp = strtotime($timestamp);
// Adjust the arguments and do whatever you want!
// Use the current time as the default
if ($timestamp === NULL) $timestamp = time();
return date($format, $timestamp);
}
使用例子:
echo display_date(); // Current time
echo display_date($user->last_login); // Formatted unix time
echo display_date('Next Monday'); // Accept strings
编写自己的函数在将来会有更大的灵活性。