Thread: DateTime Object

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17

    DateTime Object


    So, using the DateTime object, apparently within the function that contains the DateTime object declaration, one must set the default time zone every time the function is run? So, two questions:

    1) If the function that creates and uses the DateTime object is in a class, I can safely set the default time zone in the constructor function?

    2) Should I use:

    a) date_default_timezone_set($timezone);

    or

    b) new DateTimeZone($timezone) - within the DateTime function itself?

    For example:
    PHP Code:
    $firstStartDT = new DateTime($firstSegmentDate['year'] . "-" $firstSegmentDate['month'] . "-" $firstSegmentDate['day'] . "  " substr($firstSegmentStartTime,0,2) . ":" substr($firstSegmentStartTime,2,2) . ":00", new DateTimeZone($timezone)); 

    $firstSegmentStartUNIX $firstStartDT->getTimestamp(); 
    Or...
    PHP Code:
    date_default_timezone_set($timezone);

    $firstStartDT = new DateTime($firstSegmentDate['year'] . "-" $firstSegmentDate['month'] . "-" $firstSegmentDate['day'] . "  " substr($firstSegmentStartTime,0,2) . ":" substr($firstSegmentStartTime,2,2) . ":00"); 

    $firstSegmentStartUNIX $firstStartDT->getTimestamp(); 
    Last edited by Arty Zifferelli; April 16th, 2017 at 03:17 AM.
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,187
    Rep Power
    9644
    Originally Posted by Arty Zifferelli
    So, using the DateTime object, apparently within the function that contains the DateTime object declaration, one must set the default time zone every time the function is run?
    The timezone will affect how the time string is interpreted and will default to the current timezone (ie, from date_default_timezone_get). So you don't necessarily need to provide it.

    Originally Posted by Arty Zifferelli
    1) If the function that creates and uses the DateTime object is in a class, I can safely set the default time zone in the constructor function?
    Whose constructor? DateTime's? Or your own?

    Actually either way the answer is the same: it doesn't matter where your code lives. What does matter is where you want to specify the timezone...

    Originally Posted by Arty Zifferelli
    2) Should I use:

    a) date_default_timezone_set($timezone);

    or

    b) new DateTimeZone($timezone) - within the DateTime function itself?
    The first option sets the default for the remainder of PHP's execution (unless you change it again). Every date() and mktime() and whatever will be affected by that timezone.
    The second option sets the timezone for that particular DateTime. The fact that you have this as a variable suggests that it's not the sort of thing that should lapply to all your code and only matters at that particular moment.

    You probably should use the second option.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17

    More


    Well...

    I know we talked about how it is not necessary to wrap everything in a class just for the sake of being "oop".

    However, I decided to put a bunch of customized "Time Functions" that do various things into a class because within the functions themselves there are interdependencies - many of the functions that do high-level things use a handful of functions that do low-level things, and most of the code that calls the class uses many of the functions within. I'd just rather abstract it to a class rather than deal with weirdness when updating some piece of code...

    Anyway,

    The timezone I'm interested in for some particular calculation may not be the timezone the software itself runs on, they should be independent in all cases. For example this particular package runs on some server at Scott AFB, in Illinois, but is used at several widely different geographic locations.

    So my "Time Functions" class needs to deal with the time / DST / Z-time Offset where the user is, not the server.
    Last edited by Arty Zifferelli; April 16th, 2017 at 04:22 PM.
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,187
    Rep Power
    9644
    Then use the timezone in the DateTime object itself.

    What you're describing actually kinda makes sense as an object.
    PHP Code:
    class TimeOperationsWhateverThing {

        private 
    $dt null;

        public function 
    __construct($time$timezone) {
            
    $this->dt = new \DateTime($time, new DateTimeZone($timezone));
        }

        public function 
    doWhatever1() {
            
    // stuff
        
    }

        public function 
    doWhatever2() {
            
    // stuff
        
    }


    Essentially you're wrapping DateTime (so much so that it might be better to extend it...) and providing a set of functions on top of it.

IMN logo majestic logo threadwatch logo seochat tools logo