PHP Zend 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6201768/
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
PHP Zend date format
提问by maniclorn
I want to input a timestamp in below format to the database.
我想将以下格式的时间戳输入到数据库中。
yyyy-mm-dd hh:mm:ss
How can I get in above format?
我怎样才能获得上述格式?
When I use
当我使用
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
它返回 月 dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyyformat
我还使用 JavaScript 日历插入选定的日期,并以dd-mm-yyyy格式返回
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
现在,我想将这两种格式转换为 yyyy-mm-dd hh:mm:ss 以便可以插入到数据库中。由于日期格式与数据库字段格式不匹配,因此不会插入日期,仅填充 * 00-00-00 00:00:00*
Thanks for answer
谢谢回答
回答by stefgosselin
Not sure if this will help you, but try using:
不确定这是否会对您有所帮助,但请尝试使用:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
回答by Gordon
Technically, @stefgosselin gave the correct answerfor Zend_Date
, but Zend_Date
is completely overkill for just getting the current time in a common format. Zend_Date
is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date
output (and you apparently dont), stay away from it.
从技术上讲,@stefgosselin给出了正确的答案的Zend_Date
,但Zend_Date
完全是大材小用刚开当前时间的通用格式。Zend_Date
与 PHP 的本机日期相关扩展相比,使用起来非常缓慢和麻烦。如果您的Zend_Date
输出不需要翻译或本地化(而您显然不需要),请远离它。
Use PHP's native date
functionfor that, e.g.
为此使用PHP 的本机date
函数,例如
echo date('Y-m-d H:i:s');
echo date_format(date_create(), 'Y-m-d H:i:s');
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
不要犯下使用 Zend Frameworks 提供的每个组件的常见错误,因为它提供了它。绝对没有必要这样做,事实上,如果您可以使用本机 PHP 扩展以更少或相当的努力实现相同的结果,那么您最好使用本机解决方案。
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
另外,如果您要在数据库中保存日期,您是否在数据库中使用了任何与DateTime 相关的列?假设您使用的是 MySql,您可以使用 Timestamp 列或 ISO8601 Date 列。
回答by Fatmuemoo
This is how i did it:
我是这样做的:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
这样你就不需要关心它是否实际上是一个 zend 日期的实例,你可以传入一个字符串或任何其他日期。
回答by lothar
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
使用 Zend Date 的一种简单方法是在其业务对象中创建特定函数,允许将此函数参数化为日期格式。你可以找到这个地址的一个很好的例子http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
回答by akubabas
this is i did it :Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
这是我做到的:Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
这种格式的输出是“24-03-2012 13:02:01”
,你可以修改你的日期格式
回答by Lionel Morrison
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss');
method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
$date->__toString('YYYY-MM-dd HH-mm-ss');
过去我一直使用方法,但今天没有用。我得到了“2013 年 11 月 1 日 12:19:23 PM”的默认输出
So today I used $date->get('YYYY-MM-dd HH-mm-ss');
as mentioned above. Seems to have solved my problem.
所以今天我使用$date->get('YYYY-MM-dd HH-mm-ss');
了上面提到的。似乎解决了我的问题。
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html
您可以在此处找到有关输出格式的更多信息:http: //framework.zend.com/manual/1.12/en/zend.date.constants.html