Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Date in proper format in symfony2 writing own console Command

Tags:

php

symfony

How to get Date in proper format in symfony2 writing own console Command

$plantype = $allDbName->getPlanType();
$planEndOn = $allDbName->getNextPaymentDate();
$p = $planEndOn->format('H:i:s \O\n Y-m-d');
$currentDate = new \DateTime();

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

$output->writeln($date);

getting error in console

enter image description here

like image 788
Jatinder Kaur Avatar asked Apr 22 '15 10:04

Jatinder Kaur


3 Answers

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

You should do something like this instead

$planEndOn = $allDbName->getNextPaymentDate();
$planEndOn->modify('-5 days');
$output->writeln($planEndOn->format('H:i:s \O\n Y-m-d'));
like image 102
Ulti Avatar answered Oct 18 '22 07:10

Ulti


The errormessage is clear,

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

expects $p to be a dateTime Object

but at this point its a string because you already formatted as string 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 Y-m-d');

$output->writeln($date);
like image 31
john Smith Avatar answered Oct 18 '22 07:10

john Smith


I got solution

$planEndOn = $allDbName->getNextPaymentDate() ? $allDbName->getNextPaymentDate()->format('Y-m-d') : 0;

like image 2
Jatinder Kaur Avatar answered Oct 18 '22 07:10

Jatinder Kaur