Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility function to handle dates timezone conversions #108

Open
unfulvio opened this issue Nov 6, 2015 · 5 comments
Open

Add utility function to handle dates timezone conversions #108

unfulvio opened this issue Nov 6, 2015 · 5 comments
Assignees
Labels

Comments

@unfulvio
Copy link
Member

unfulvio commented Nov 6, 2015

Most of the time we store dates and time in UTC. But then we have cases were the site timezone is disregarded (Memberships for example) and thus there might be some discrepancy in what the users see (thinking that's date-time in their timezone) and what the extension actually handles, a different time offset.

Storing time in UTC is fine, but we might want to have a shared function to help out with timezone offsets. Eventually we might also want to convert one date in one timezone to another.

So we have four factors: date-time, date-time format, timezone to convert from (UTC or other?), timezone to convert to.

For memberships I was going for something along these lines but it can be adapted (this function assumes the date format Memberships uses and also it only converts from UTC)

function wc_memberships_adjust_utc_date_by_timezone( $date, $format = 'Y-m-d H:i:s', $timezone = '' ) {

    if ( is_int( $date ) ) {
        $date = date( $format, $date );
    }

    if ( is_int( $date ) ) {
        $src_date = date( $format, $date );
    } else {
        $src_date = $date;
    }

    $timezone  = new DateTimeZone( $timezone );
    $adj_date  = new DateTime( $src_date, new DateTimeZone( 'UTC' ) );
    $offset    = $timezone->getOffset( $adj_date );
    // getTimestamp method not used here for PHP 5.2 compatibility
    $timestamp = intval( $adj_date->format( 'U' ) );

    return is_int( $date ) ? $timestamp + $offset : date( $format, $timestamp + $offset );
}
@unfulvio unfulvio added this to the 4.2 milestone Nov 6, 2015
@unfulvio
Copy link
Member Author

unfulvio commented Nov 9, 2015

thinking about it, since @maxrice mentioned the idea of copying some Carbon ideas into the framework, the feature mentioned in this issue could be part of that - it may be worth if handling dates is a very recurring thing among many plugins

@maxrice
Copy link
Contributor

maxrice commented Nov 9, 2015

Cool. I think the 1st step here is a survey of what timezone-related code we have in our extensions so we can figure out what would be most helpful to abstract into a framework helper.

@maxrice maxrice modified the milestones: 4.3, 4.2 Dec 9, 2015
@maxrice maxrice modified the milestones: Future Release, 4.3 Mar 14, 2016
@unfulvio unfulvio self-assigned this Apr 1, 2016
@ChaseWiseman ChaseWiseman modified the milestone: Future Release Oct 13, 2016
@ragulka
Copy link
Contributor

ragulka commented Feb 21, 2017

I'll add my vote for this, since I'm basically doing all the same date manipulations, formatting, parsing in PDF Vouchers as well :)

@ChaseWiseman ChaseWiseman added this to the 4.7.0 milestone Feb 21, 2017
@ChaseWiseman ChaseWiseman modified the milestones: v4.7.0, v5.0.0 Jul 26, 2017
@unfulvio
Copy link
Member Author

another use case appeared in Local Pickup Plus, as setting a pickup deadline or lead time needed time zone handling to ensure the right days on the pickup appointment calendar were selectable

the fix consisted of adopting a similar solution as Memberships to adjust a timestamp by timezone

@ChaseWiseman ChaseWiseman modified the milestone: v5.0.0 Aug 29, 2017
@ChaseWiseman ChaseWiseman added this to the v5.1.0 milestone Nov 17, 2017
@ChaseWiseman ChaseWiseman modified the milestones: v5.1.0, v5.2.0 Feb 2, 2018
@ChaseWiseman ChaseWiseman removed this from the v5.2.0 milestone Jun 12, 2018
@unfulvio
Copy link
Member Author

unfulvio commented Aug 7, 2018

Note: given the FW is now using PHP 5.3, and soon 5.4, perhaps in plugins we should rather switch to use native DateTime objects. We can even include Carbon in the Framework, perhaps namespaced (perhaps with Mozart? it's not a huge library anyway), to ease date-time operations with its very fluent methods. Version 1.x of the library is very stable and only requires 5.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants