我使用PHP中的DateTime对象来获得一定范围的日期PHP日期时间值无法传递的参数
的但是我无法在DateTime对象的值传递到一个函数。
当我尝试将其分配给一个变量,该变量为空
代码为初级讲座
$date1 = new DateTime('2013-04-11');
$test = $date1->setTime($i, 59);
echo $test;
任何人我怎么了$ DATE1分配给一个变量任何想法?
我使用PHP中的DateTime对象来获得一定范围的日期PHP日期时间值无法传递的参数
的但是我无法在DateTime对象的值传递到一个函数。
当我尝试将其分配给一个变量,该变量为空
代码为初级讲座
$date1 = new DateTime('2013-04-11');
$test = $date1->setTime($i, 59);
echo $test;
任何人我怎么了$ DATE1分配给一个变量任何想法?
返回一个DateTime
对象,该对象没有toString
方法。
以上会产生一个致命的错误(你真的应该enable errors):
Catchable fatal error: Object of class DateTime could not be converted to string in /code/KHCgOS on line 7
PHP Catchable fatal error: Object of class DateTime could not be converted to string in /code/KHCgOS on line 7
你应该这样做:
echo $test->format('H:i:s d-m-Y');
更多信息:http://codepad.viper-7.com/TNHGlj
注:一个生产机器,你也应该启用错误报告,但不要显示它们,你应该记录它们
使用http://www.php.net/manual/en/datetime.format.php
$date1 = new DateTime('2013-04-11');
$date1->setTime($i, 59);
echo $date1->format('Y-m-d H:i:s');
从DateTime::setTime function documentation,它返回一个DateTime对象,所以为了打印的价值,你必须使用DateTime::format function。下面是一个工作示例:
<?php
$i = 14;
$date1 = new DateTime('2013-04-11');
$date1->setTime($i, 59);
echo $date1->format('Y-m-d H:i:s') . "\n";
回报
2013-04-11 14:59:00
http://php.net/manual/en/datetime.settime.php – PeeHaa 2013-04-25 09:11:40