Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class DateTime not found

Missing something when declaring a DateTime object in PHP 5.3.8

I get a JSON string with a determinate date time which is passed to my php controller.

For some reason, I am not getting it to be mapped as a DateTime object in php. But sort of weirdly. See the following images:

  1. As you can see in the Expressions Window (top right), BEFORE the step, I am checking that new DateTime(myVariable) is bringing and transforming correctly what I need. In the first watch, the variable to pass to DateTime constructor. In the second watch, the expression newDateTime(myVariable) already mapped as a DateTimeObject. Apparently fine up to here.

    enter image description here

  2. But, sadly, when I go forward and press F6, the following exemption (see also the image below) is thrown:

    Fatal error: Class 'Acme\StoreBundle\Repository\DateTime' not found in /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Repository/HistoryRepository.php on line 19  Call Stack:     0.0201     693568   1. {main}() /Users/pgbonino/Sites/Symfony/web/app_dev.php:0     0.0267    2106576   2. Symfony\Component\HttpKernel\Kernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/web/app_dev.php:24     0.0377    2649176   3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/bootstrap.php.cache:547     0.0378    2650832   4. Symfony\Component\HttpKernel\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:4879     0.0378    2650832   5. Symfony\Component\HttpKernel\HttpKernel->handleRaw(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3875     0.1574    5562232   6. call_user_func_array(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905     0.1574    5562600   7. Acme\StoreBundle\Controller\HistoryController->saveTestAction() /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905     0.1694    5739032   8. Acme\StoreBundle\Repository\HistoryRepository->saveTestInHistory(???, ???) /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Controller/HistoryController.php:33 

    enter image description here

So, strangely, the Watch Expressions window from Eclipse is not working just like the execution engine and/or vice-versa.

Of course, I'd prefer it to be the opposite (It worked in the execution and not in the watch window :) ).

So, any idea?

like image 314
ElPiter Avatar asked May 31 '12 22:05

ElPiter


People also ask

How do you use DateTime class?

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.

What is new DateTime in PHP?

The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format.

How can I get current date and time in PHP?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

What Date time PHP function will I use if I want to calculate the date 30 days from today?

echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) .


1 Answers

You're currently in the Acme\StoreBundle\Repository\DateTime napespace. To address to the default namespace in this case you need to put leading \ before your classname, like

$dt = new \DateTime(...); 

so

namespace foo; $obj = new class(); 

will try to find class definition within foo namespace.

And

namespace foo; $obj = new \class(); 

will try to find class definition within global namespace.

As an alternative you could import the class using

use \DateTime; 

or create alias (in case if you already have the class with the same name in current NS):

use \DateTime as NewDT; 
like image 80
zerkms Avatar answered Sep 23 '22 02:09

zerkms