9

I'm trying to use the DateTime class to output the current time in DateTime::ATOM format.

I'm not sure if I'm even using it correctly. Or do I have to import a library or maybe turn on a wamp php module.

I'm getting "syntax error, unexpected T_NEW" error

here is the code:

<?php
function d()
{

     $df =  new DateTime(DateTime::ATOM);
    echo $df;
}

?>

2 Answers 2

15

You'd use DateTime like this:

$time = new DateTime;
echo $time->format(DateTime::ATOM);

The constructor (new DateTime) expects the time you want to create the object for, the format doesn't matter at this point. You specify the format when outputting the time.

Having said that, the error you're getting seems pretty unrelated and may not have anything to do with that specific line.

7
  • Thank you, well, I'm getting this error now : Fatal error: Class 'DateTime' not found. I'm using Wamp 5 with: Apache version : Apache/1.3.31 (Win32) PHP version : 5.0.2 MySQL version : 4.0.21-nt-log
    – airnet
    Jul 2, 2011 at 3:00
  • @airnet You should seriously upgrade your PHP (and MySQL) version, 5.0.2 is very much obsolete and does not support DateTime by default. You can use date(DATE_ATOM) instead.
    – deceze
    Jul 2, 2011 at 3:05
  • new DateTime(); need parenthese Jul 2, 2011 at 3:06
  • Thanks Doak, I added the (). But the error still remains. Thanks Deceze, I will upgrade. :)
    – airnet
    Jul 2, 2011 at 3:07
  • 2
    @Daok Try it, it works without! Parentheses are only required when supplying arguments to the constructor. The constructor is always called regardless.
    – deceze
    Jul 2, 2011 at 3:09
2

Use :

 $x = date(DATE_ATOM, strtotime('2009-11-04T19:55:41Z'));

or

 $x = date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.