[wp-hackers] Date/Time and WP

Jacob Snyder jacobsnyder at gmail.com
Wed Jul 17 16:11:53 UTC 2013


I have found that although date_i18n() is awesome, I also need to create my
timestamps using current_time('timestamp').

I wasn't getting timezone support from just date_i18n, until I started
using current_time. I haven't put much thought into, though, once my times
started getting recorded and retrieved as expected.

On Wed, Jul 17, 2013 at 8:14 AM, <wp-hackers-request at lists.automattic.com>wrote:

> Send wp-hackers mailing list submissions to
>         wp-hackers at lists.automattic.com
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.automattic.com/mailman/listinfo/wp-hackers
> or, via email, send a message with subject or body 'help' to
>         wp-hackers-request at lists.automattic.com
>
> You can reach the person managing the list at
>         wp-hackers-owner at lists.automattic.com
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of wp-hackers digest..."
>
>
> Today's Topics:
>
>    1. Date/Time and WP (Dobri)
>    2. Re: Date/Time and WP (Ryan McCue)
>    3. Re: Date/Time and WP (William P. Davis)
>    4. Re: Date/Time and WP (Dobri)
>    5. Re: Date/Time and WP (Dobri)
>    6. Re: Date/Time and WP (Alex King)
>    7. Re: Date/Time and WP (Ryan McCue)
>    8. Re: Date/Time and WP (Dobri)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 17 Jul 2013 08:45:12 -0400
> From: Dobri <dyordan1 at ramapo.edu>
> To: "Discussion list for WordPress developers."
>         <wp-hackers at lists.automattic.com>
> Subject: [wp-hackers] Date/Time and WP
> Message-ID: <B5C98932-EC55-4E65-9FD5-CEB4BF039F70 at ramapo.edu>
> Content-Type: text/plain;       charset=us-ascii
>
>
> Hey guys,
>
> From my understanding, when dealing with date and time, WordPress, because
> of PHP 4 legacy support, sets timezone to UTC and does its own calculations
> based on the timezone in options. So, when writing plugins, what would be
> the correct approach to making sure correct timezone is used? Should I use
> built-in functions like date and somehow specify timezone every time I
> access them? Or are there any WordPress functions created for that purpose?
> Any help with this appreciated. Thanks!
>
> ~Dobri
>
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 17 Jul 2013 22:48:02 +1000
> From: Ryan McCue <lists at rotorised.com>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <51E69282.3020907 at rotorised.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Dobri wrote:
>
> > From my understanding, when dealing with date and time, WordPress,
> because of PHP 4 legacy support, sets timezone to UTC and does its own
> calculations based on the timezone in options. So, when writing plugins,
> what would be the correct approach to making sure correct timezone is used?
> Should I use built-in functions like date and somehow specify timezone
> every time I access them? Or are there any WordPress functions created for
> that purpose?
>
> See http://core.trac.wordpress.org/ticket/24730
>
> Here's how I handle it:
>
> protected function get_timezone() {
>         $tzstring = get_option( 'timezone_string' );
>         if ( ! $tzstring ) {
>                 // Create a UTC+- zone if no timezone string exists
>                 $current_offset = get_option( 'gmt_offset' );
>                 if ( 0 == $current_offset )
>                         $tzstring = 'UTC';
>                 elseif ($current_offset < 0)
>                         $tzstring = 'Etc/GMT' . $current_offset;
>                 else
>                         $tzstring = 'Etc/GMT+' . $current_offset;
>         }
>         $zone = new DateTimeZone( $tzstring );
>         return $zone;
> }
>
> --
> Ryan McCue
> <http://ryanmccue.info/>
>
>
> ------------------------------
>
> Message: 3
> Date: Wed, 17 Jul 2013 12:56:47 +0000
> From: "William P. Davis" <will.davis at gmail.com>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID:
>
> <1498213418-1374065808-cardhu_decombobulator_blackberry.rim.net-739188336- at b16.c4.bise6.blackberry
> >
>
> Content-Type: text/plain
>
> date_i18n is a fantastic function that will make sure the date and time is
> returned in the same timezone set in the WP settings
> Sent from my BlackBerry
>
> -----Original Message-----
> From: Ryan McCue <lists at rotorised.com>
> Sender: "wp-hackers" <wp-hackers-bounces at lists.automattic.com>Date: Wed,
> 17 Jul 2013 22:48:02
> To: <wp-hackers at lists.automattic.com>
> Reply-To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
>
> Dobri wrote:
>
> > From my understanding, when dealing with date and time, WordPress,
> because of PHP 4 legacy support, sets timezone to UTC and does its own
> calculations based on the timezone in options. So, when writing plugins,
> what would be the correct approach to making sure correct timezone is used?
> Should I use built-in functions like date and somehow specify timezone
> every time I access them? Or are there any WordPress functions created for
> that purpose?
>
> See http://core.trac.wordpress.org/ticket/24730
>
> Here's how I handle it:
>
> protected function get_timezone() {
>         $tzstring = get_option( 'timezone_string' );
>         if ( ! $tzstring ) {
>                 // Create a UTC+- zone if no timezone string exists
>                 $current_offset = get_option( 'gmt_offset' );
>                 if ( 0 == $current_offset )
>                         $tzstring = 'UTC';
>                 elseif ($current_offset < 0)
>                         $tzstring = 'Etc/GMT' . $current_offset;
>                 else
>                         $tzstring = 'Etc/GMT+' . $current_offset;
>         }
>         $zone = new DateTimeZone( $tzstring );
>         return $zone;
> }
>
> --
> Ryan McCue
> <http://ryanmccue.info/>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> ------------------------------
>
> Message: 4
> Date: Wed, 17 Jul 2013 08:57:49 -0400
> From: Dobri <dyordan1 at ramapo.edu>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <B7014BA8-D40A-4E11-9561-AB14E66F1E29 at ramapo.edu>
> Content-Type: text/plain;       charset=us-ascii
>
> Thanks Ryan! When would a timezone_string not exist though? Wouldn't that
> simply mean settings were not touched, therefore it's some default (I'd
> guess UTC?)
>
> ~Dobri
>
> On Wed, 17 Jul 2013, at 8:48 AM, Ryan McCue wrote:
>
> > Dobri wrote:
> >
> >> From my understanding, when dealing with date and time, WordPress,
> because of PHP 4 legacy support, sets timezone to UTC and does its own
> calculations based on the timezone in options. So, when writing plugins,
> what would be the correct approach to making sure correct timezone is used?
> Should I use built-in functions like date and somehow specify timezone
> every time I access them? Or are there any WordPress functions created for
> that purpose?
> >
> > See http://core.trac.wordpress.org/ticket/24730
> >
> > Here's how I handle it:
> >
> > protected function get_timezone() {
> >       $tzstring = get_option( 'timezone_string' );
> >       if ( ! $tzstring ) {
> >               // Create a UTC+- zone if no timezone string exists
> >               $current_offset = get_option( 'gmt_offset' );
> >               if ( 0 == $current_offset )
> >                       $tzstring = 'UTC';
> >               elseif ($current_offset < 0)
> >                       $tzstring = 'Etc/GMT' . $current_offset;
> >               else
> >                       $tzstring = 'Etc/GMT+' . $current_offset;
> >       }
> >       $zone = new DateTimeZone( $tzstring );
> >       return $zone;
> > }
> >
> > --
> > Ryan McCue
> > <http://ryanmccue.info/>
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>
>
> ------------------------------
>
> Message: 5
> Date: Wed, 17 Jul 2013 09:01:24 -0400
> From: Dobri <dyordan1 at ramapo.edu>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <8FEF3A29-3708-410C-B94D-DAD2624598DD at ramapo.edu>
> Content-Type: text/plain;       charset=us-ascii
>
> Awesome, exactly what I was looking for. Thanks William! Just a sanity
> check, timestamps are just timestamps and are not affected by timezones,
> correct? As in, I can still use time(), time()+DAY_IN_SECONDS, etc.?
>
> ~Dobri
>
> On Wed, 17 Jul 2013, at 8:56 AM, William P. Davis wrote:
>
> > date_i18n is a fantastic function that will make sure the date and time
> is returned in the same timezone set in the WP settings
> > Sent from my BlackBerry
> >
> > -----Original Message-----
> > From: Ryan McCue <lists at rotorised.com>
> > Sender: "wp-hackers" <wp-hackers-bounces at lists.automattic.com>Date:
> Wed, 17 Jul 2013 22:48:02
> > To: <wp-hackers at lists.automattic.com>
> > Reply-To: wp-hackers at lists.automattic.com
> > Subject: Re: [wp-hackers] Date/Time and WP
> >
> > Dobri wrote:
> >
> >> From my understanding, when dealing with date and time, WordPress,
> because of PHP 4 legacy support, sets timezone to UTC and does its own
> calculations based on the timezone in options. So, when writing plugins,
> what would be the correct approach to making sure correct timezone is used?
> Should I use built-in functions like date and somehow specify timezone
> every time I access them? Or are there any WordPress functions created for
> that purpose?
> >
> > See http://core.trac.wordpress.org/ticket/24730
> >
> > Here's how I handle it:
> >
> > protected function get_timezone() {
> >       $tzstring = get_option( 'timezone_string' );
> >       if ( ! $tzstring ) {
> >               // Create a UTC+- zone if no timezone string exists
> >               $current_offset = get_option( 'gmt_offset' );
> >               if ( 0 == $current_offset )
> >                       $tzstring = 'UTC';
> >               elseif ($current_offset < 0)
> >                       $tzstring = 'Etc/GMT' . $current_offset;
> >               else
> >                       $tzstring = 'Etc/GMT+' . $current_offset;
> >       }
> >       $zone = new DateTimeZone( $tzstring );
> >       return $zone;
> > }
> >
> > --
> > Ryan McCue
> > <http://ryanmccue.info/>
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>
>
> ------------------------------
>
> Message: 6
> Date: Wed, 17 Jul 2013 07:03:29 -0600
> From: Alex King <lists at alexking.org>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <F62400B3-7B4B-4C8A-878F-4E3EE72A2B6D at alexking.org>
> Content-Type: text/plain; charset=us-ascii
>
> Be aware of this too: http://core.trac.wordpress.org/ticket/20328
>
> Cheers,
> --Alex
>
> http://alexking.org | http://crowdfavorite.com
>
>
>
>
> ------------------------------
>
> Message: 7
> Date: Wed, 17 Jul 2013 23:06:11 +1000
> From: Ryan McCue <lists at rotorised.com>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <51E696C3.4040102 at rotorised.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Dobri wrote:
> > Thanks Ryan! When would a timezone_string not exist though? Wouldn't
> that simply mean settings were not touched, therefore it's some default
> (I'd guess UTC?)
>
> If the timezone is set to a manual offset (e.g. UTC+10), then the
> timezone string won't be set (IIRC). In addition, older versions of
> WordPress won't have the timezone_string setting and use gmt_offset
> instead.
>
> --
> Ryan McCue
> <http://ryanmccue.info/>
>
>
> ------------------------------
>
> Message: 8
> Date: Wed, 17 Jul 2013 09:14:13 -0400
> From: Dobri <dyordan1 at ramapo.edu>
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] Date/Time and WP
> Message-ID: <883DA041-BEE1-4F67-9DEF-3710A1676D77 at ramapo.edu>
> Content-Type: text/plain;       charset=us-ascii
>
> That makes sense. So, would you say there are performance benefits to
> using DateTime and the function you provided to just using date_i18n as
> William suggested? And is this a more robust method (for now) because of
> the bug in core that Alex brought up when dealing with date/time in DLS
> while it's currently not DLS and vice versa?
>
> ~Dobri
>
> On Wed, 17 Jul 2013, at 9:06 AM, Ryan McCue wrote:
>
> > Dobri wrote:
> >> Thanks Ryan! When would a timezone_string not exist though? Wouldn't
> that simply mean settings were not touched, therefore it's some default
> (I'd guess UTC?)
> >
> > If the timezone is set to a manual offset (e.g. UTC+10), then the
> > timezone string won't be set (IIRC). In addition, older versions of
> > WordPress won't have the timezone_string setting and use gmt_offset
> instead.
> >
> > --
> > Ryan McCue
> > <http://ryanmccue.info/>
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>
> ------------------------------
>
> End of wp-hackers Digest, Vol 102, Issue 19
> *******************************************
>


More information about the wp-hackers mailing list