We help IT Professionals succeed at work.

What is being returned when you invoke the DateTime class?

Bruce Gust
Bruce Gust asked
on
71 Views
Last Modified: 2017-03-25
Here's what I understand:

The DateTime class returns a date. Is it a superglobal? I get the impression that it's systemic, so what's the term you use to describe it?

When you invoke the DateTime class, what is being returned? If I do this:

$date = new DateTime();
//echo $date->format("Y-m-d");
echo $date;

I get an error that says the object of class DateTime could not be converted to a string. That tells me that DateTime by itself has to massaged in order for it to be used programmatically. From what I can tell, you have to format it in order for it to resonate as accurate syntax.

I've looked at the manual, but I'm still coming up short. I'm working in a .NET shop and I'm seeing several instances where some well meaning programmers have invoked the DateTime class, but they've not formatted it correctly which has led to several errors. I want to be able to explain why it's failing in conjunction with coding it correctly.

So, what is the DateTime class and why is it that you have to format it in order to be able to echo it / use it in other functionality?
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Bruce GustPHP Developer

Author

Commented:
But Ray, what is it that makes it systemic? I recognize the class and instantiating etc, but instead of having to write something like this:

<?php

class TheDate {
      
      function __construct() {
            $today=date("Y-m-d");
            echo $today;
      }

}
$date = new TheDate();
      
?>

It seems like all of that is automatically assumed simply by writing $date=new DateTime(). That's why I was wondering if it fell into a special category since you're not having to manually document a construct within the DateTime class etc.

And the other thing that I wanted to confirm is that you have to format the output of the DateTime class before you attempt to utilize it in any additional syntax.

For example, if you tried this:

$lastsyncdatetime=new DateTime();
date_add($lastsyncdatetime, date_interval_create_from_date_string('-30 days'));

That's going to fail because $lastsyncdatetime isn't a string.

In order for that computation to work (date_interval_create_from_date_string), you first have to convert $lastsyncdatetime to a string.

But how and why?
CERTIFIED EXPERT
Top Expert 2010

Commented:
DateTime() returns an object, which is why you can't just use it as if it were a string.

And I'm not understanding your question regarding the construct.  If you don't pass in a date string to the class, it is instantiated with a default of "now" and no time zone (which is why I mentioned that you should be aware of time zone when reviewing date time calls)

http://php.net/manual/en/datetime.construct.php
Most Valuable Expert 2011
Author of the Year 2014

Commented:
what is it that makes it systemic?
It's a built-in PHP class.  PHP has many built-in classes, just as PHP has many built-in functions.  You don't have to write anything special to use the built-in language features -- just follow the directions in the manual.
format the output of the DateTime class before you attempt to utilize it in any additional syntax.
Not really - the DateTime objects have methods that allow them to interact with each other.  IIRC you can compare two DateTime objects with an if() statement and inequality to see if the values are in the past or future, etc.  I do not recommend comparing them for equality, for reasons that are explained in the article.

zephyr_hex (Megan) makes an important point about the default values of the class.  It's not really "no time zone;" observed behavior is that in the absence of your specification PHP will choose UTC as a default time zone.  The objects will still work correctly, but formatted output will reflect Greenwich Mean Time unless and until you tell the objects to format the time for a different zone.  See Setting Your Own Clock Values in the referenced article.

I think you might want to work with some of the examples in the article (clone, hack, repeat).  You can always use var_dump() to print out any of the DataTime objects, and to print out the results from using them in different ways.  One caution: Choose and use the object-oriented examples, not the procedural examples.  Your code will be much easier to test if you use the OOP approach.  Do not try to mix OOP and procedural examples.  Stick to the OOP examples and you'll be fine.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
stale question
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.