How to get the date in the correct format in symfony2, writing your own console. Team

How to get the date in the correct format in symfony2, writing your own console Command

$plantype = $allDbName->getPlanType(); $planEndOn = $allDbName->getNextPaymentDate(); $p = $planEndOn->format('H:i:s \O\n Ym-d'); $currentDate = new \DateTime(); $date = date_modify($p, '-5 day'); $output->writeln($date); 

error in console

enter image description here

+8
php symfony
source share
3 answers

I got a solution

$ planEndOn = $ allDbName-> getNextPaymentDate ()? $ allDbName-> getNextPaymentDate () β†’ format ('Ym-d'): 0;

+2
source share

DateTime::format() returns a string, so $p is a string, not a DateTime.

Instead, you should do something like this

 $planEndOn = $allDbName->getNextPaymentDate(); $planEndOn->modify('-5 days'); $output->writeln($planEndOn->format('H:i:s \O\n Ym-d')); 
+5
source share

Erroneous message is clear

 date_modify($p, '-5 day'); 

expects $p be a DateTime object

but at this point it is a line because you are already formatted as a line with ->format() so change the order of your script:

 $plantype = $allDbName->getPlanType(); $planEndOn = $allDbName->getNextPaymentDate(); $p = date_modify($planEndOn, '-5 day'); $date = $p->format('H:i:s \O\n Ym-d'); $output->writeln($date); 
+2
source share

All Articles