Bug report: Dates and Times have incorrect timezone offsets.
-
Hi!
Hope you’re doing well!
I have a bug I’d like to report regarding incorrectly formatted dates and times displayed throughout the plugin. The easiest way to demonstrate this is on the Donor Dashboard. On the Dashboard, users are presented with their “Last donated {TIME} ago”. Go ahead add a test donation for that user now and you’ll notice how this time difference is incorrect.
The issue has to do with the fact that all dates stored in the
give_donationmeta
table are being saved in the site’s timezone, rather than in UTC. (This includes the meta ‘_give_payment_date’, the data in ‘_give_payment_meta’, and ‘_give_completed_date’.) As such, functions likestrtotime()
, which assume that they’re working with UTC, end up incorrectly formatting the date and adding the local timezone offset.Technically, the fix for this is pretty easy. The simple way to fix it is to tell functions like strtotime() that the string they’re dealing with is already in the WP Timezone.
So for example, in wp-content/plugins/give/src/DonorDashboards/Profile.php replacing line 83:
'sinceLastDonation' => ! empty( $this->donor->get_last_donation_date() ) ? human_time_diff( strtotime( $this->donor->get_last_donation_date() ) ) : '',
With this:
'sinceLastDonation' => ! empty( $this->donor->get_last_donation_date() ) ? human_time_diff( strtotime( $this->donor->get_last_donation_date() . ' ' . wp_timezone_string() ) ) : '',
The alternate solution is to keep things consistent with how WordPress and MySQL like to store dates (and how they’re already being stored in the
give_donors
table in ‘date_created’ and ‘verify_throttle’) and store them in the database in UTC. It may be worth the effort as then, should a site admin need to change a site’s timezone, this wouldn’t affect donations as they’d be displayed correctly after the change.I also noticed that you’re using the date_i18n() which is a bit of a depricated function that oftentimes ends up again formatting the date/times with the wrong offset. To see this in action, let’s temporarily change the give_date_format to include times:
add_filter( 'give_date_format', 'custom_give_date_format' ); function custom_give_date_format( $date_format_contexts ) { return 'F j, Y g:i a'; }
Now go to the Donors admin page. You’ll notice how all dates are actually being output in UTC rather than with the correct site’s timezone offset.
The fix for this is really easy: replace
date_i18n()
with the newerwp_date()
which will correctly take into account the site’s Timezone and format the dates/times accurately.Let me know your thoughts on the above!
And again, thanks for the amazing work on the plugin!
Cheers!
David
- The topic ‘Bug report: Dates and Times have incorrect timezone offsets.’ is closed to new replies.