godaddy-wordpress / godaddy-wordpress/wc-plugin-framework

Add utility function to handle dates timezone conversions

Open
#108 5 comments 1 reaction 1 assignee Claimed by @ChaseWiseman View on GitHub
Feature
Dominant language
PHP
Stars
146
Forks
43
Avg merge
21m
Merged PRs (30d)
3

Description

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 );
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.