I'm using the symfony2 framework and I want to use the PHP's DateTime class (PHP version is 5.3).
Here the declaration:
namespace SDCU\GeneralBundle\Entity; class Country { public function __construct(){ $this->insertedAt = new DateTime(); } }
But, when executing this constructor, I get an error saying that there's no "SDCU\GeneralBundle\Entity\DateTime" class. I've been searching around for DateTime's namespace but with no success... any idea?
To use the DateTime object you just need to instantiate the the class. $date = new DateTime(); The constructor of this object takes two parameters the first is the time value you want to set the value of the object, you can use a date format, unix timestamp, a day interval or a day period.
The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) .
DateTime
is in the global namespace, and as "class names always resolve to the current namespace name" you have to use \DateTime
.
Or import the package using:
use \Datetime;
Better solution for using classes in global namespaces is "use" keyword instead of "\" before class.
namespace SDCU\GeneralBundle\Entity; use \DateTime; class Country { public function __construct(){ $this->insertedAt = new DateTime(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With