5

I tried, to run the following code, on PHP v5.3.13, but still getting the class not found error:

$tZone = new DateTimeZone("Europe/Amsterdam");

How can I use DateTimeZone on 5.3.13?

hakre
  • 193,403
  • 52
  • 435
  • 836
xam
  • 452
  • 2
  • 7
  • 15
  • Read the error message you get carefully. Also if you ask about a problem, put the (full) error message as well into your quesiton. – hakre Mar 29 '15 at 00:05
  • I didn't know it was a namespace problem when I posted this question. if I did, I would have fixed it myself and not bothered to post the question. The error message I got was class not found, which could implied anything. So sorry that it is a duplicate question to you, but when I searched for my error, I couldn't locate the other post you mentioned. Even I located it, I wouldn't think that was related to my error. The other post asked for how to make namespaces work, and my post is on what caused the error message. – xam Mar 31 '15 at 16:09
  • No problem at all. We close against duplicates to keep the site in a good state. The hint on error messages was more meant generally: With your question provide as much information as available, error messages are most often important even if you think those are general. Because unless you share which error message exactly, the error is not really clearly written about. That's all. – hakre Mar 31 '15 at 18:20

1 Answers1

24

Try this:

$tZone = new \DateTimeZone("Europe/Amsterdam");
Dragony
  • 1,712
  • 11
  • 20
  • adding back slash works, a namespace problem...thanks! – xam Feb 17 '15 at 19:31
  • Yes, adding the backslash will use the global namespace, instead of the namespace defined in the file. – Dragony Feb 17 '15 at 19:33
  • 1
    DateTimeZone is in global namespaces. If you not add "\" it mean that DateTimeZone is in local namespaces. You can also "use" keyword instead of "\" before class. "use \DateTimeZone" – Gregsparrow Feb 17 '15 at 19:38