Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on 

What is being returned when you invoke the DateTime class?

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?
PHP

Avatar of undefined
Last Comment
Ray Paseur
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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?
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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

stale question
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo