简体   繁体   English

从String中设置Symfony2中的DateTime和Time

[英]Set DateTime and Time in Symfony2 from String

In my entity, i have two fields, one type DateTime and other type Time. 在我的实体中,我有两个字段,一个是DateTime,另一个是Time。

I'm trying to do this: 我正在尝试这样做:

$time = "05:45";
$date = "01-09-2015"

$entity = new Entity();
$entity->setDate(new \DateTime($date));
$entity->setTime($time);

In both case, Symfony told me that the code has problems with format. 在这两种情况下,Symfony都告诉我代码格式有问题。

I also tried: 我也尝试过:

->setDate(new Datetime($date));
->setDate(DateTime::createFromFormat($date, 'd-m-Y));
->setTime(strtotime($time));

Always with the same result. 始终具有相同的结果。

If anybody could help me, i'll be thankful. 如果有人能帮助我,我会感激的。

Thanks 谢谢


UPDATED 更新

Now, I tried 现在,我试过了

$date = strtotime($date);
$time = strtotime($time);
$entity->setDate(date("d-m-Y", $date));
$entity->setTime(date("H:i",$time));

And Symfony told me: Symfony告诉我:

Error: Call to a member function format() on a non-object 错误:在非对象上调用成员函数format()

in vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/TimeType.php at line 53   -
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return ($value !== null)
            ? $value->format($platform->getTimeFormatString()) : null;
    }
    /**

I also tried this: 我也试过这个:

$dateObject = new DateTime();
$date = $dateObject->format('d-m-Y');
$time = $dateObject->format('H:i');
$entity->setDate($date);
$entity->setTime($time);

And symfony told me: 而symfony告诉我:

Attempted to call method "format" on class "Symfony\Component\Validator\Constraints\DateTime". On line "$date = $dateObject->format('Y-m-d');"

Thanks for the help 谢谢您的帮助


Updated - Resolve 更新 - 解决

Finally, yesterday a resolved the problem, using this: 最后,昨天解决了这个问题,用这个:

$entity->new Entity();
$entity->setDate(\DateTime::createFromFormat('d-m-Y',$date));
$entity->setTime(\DateTime::createFromFormat('H:i',$time));

Thanks! 谢谢!

Symfony doesn't know where to find DateTime class, so it is searching in the wrong (default) namespace. Symfony不知道在哪里找到DateTime类,因此它在错误的(默认)命名空间中进行搜索。 Use a leading \\ . 使用领先的\\

Moreover you inverted the format argument and the time argument in the createFromFormat() function. 此外,您在createFromFormat()函数中反转了format参数和time参数。

So your code becomes: 所以你的代码变成:

$time = "05:45";
$date = "01-09-2015"

$entity = new Entity();
$entity->setDate(\DateTime::createFromFormat('d-m-Y', $date));
$entity->setTime(\DateTime::createFromFormat('H:i', $time));

By the way, are you sure you really need to split Date and Time in your entity? 顺便说一句,你确定你真的需要在你的实体中分割日期和时间吗? It depends on your case but you could do this instead (only one object to manipulate): 这取决于你的情况,但你可以这样做(只需要操作一个对象):

$time = "05:45";
$date = "01-09-2015"

$entity = new Entity();
$entity->setDateTime(\DateTime::createFromFormat('d-m-Y H:i', $date.' '.$time));

Check out http://php.net/manual/de/datetime.createfromformat.php to be sure that your format fits. 查看http://php.net/manual/de/datetime.createfromformat.php以确保您的格式适合。

Or check mktime(hour,minute,second,month,day,year) Or check strtotime(time,now) eg here http://www.w3schools.com/php/php_date.asp 或者检查mktime(小时,分钟,秒,月,日,年)或检查strtotime(时间,现在),例如http://www.w3schools.com/php/php_date.asp

Do it like this: 像这样做:

$dateObject = new DateTime();
$date = $dateObject->format('Y-m-d');
$time = $dateObject->format('H:i:s'); // or $time = $dateObject->format('H:i'); if you dont want to have the seconds

$entity = new Entity();
$entity->setDate($date);
$entity->setTime($time);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM