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

gppa-acf-date-formatter.php: Fixed date formatting issue caused by timezone offset resulting in a one-day discrepancy. #1009

Merged

Conversation

malayladu
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2826054984/76967

Similar ticket: https://secure.helpscout.net/conversation/2601997622/66482

Summary

While using ACF & this snippet together resulting in a one-day discrepancy.

This issue was related to timezone.

Here's the customers screencast explains this issue.

https://video.tsttechnology.com/conversations/cf879d60-9004-5cec-99c4-0d8fde4dba88

@malayladu malayladu self-assigned this Jan 24, 2025
Copy link
Contributor

@claygriffiths claygriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, but I do wonder if this will create issues if the PHP timezone is set to the WP timezone instead of UTC?

I assume that the server timezone is UTC so the time coming from strtotime is also UTC. Local defaults to UTC in php.ini.hbs.

Thoughts on something like...

$timezone = new DateTimeZone( 'UTC' );

try {
    // Attempt to parse the date value
    $date = new DateTime( $template_value );
    $date->setTimezone( $timezone ); // Ensure it's parsed as UTC
    return wp_date( 'd/m/Y', $date->getTimestamp(), $timezone );
} catch (Exception $e) {
    // Handle invalid date formats gracefully
    return $template_value;
}

@malayladu
Copy link
Contributor Author

@claygriffiths This is better implementation. It handles errors gracefully and ensures consistent behaviour.

…timezone offset resulting in a one-day discrepancy.
@malayladu malayladu force-pushed the malay/fix/76967-gppa-acf-date-formatter-timzone-issue branch from add8056 to 90c26aa Compare January 28, 2025 11:00
Copy link
Contributor

@claygriffiths claygriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@malayladu malayladu merged commit 4834ece into master Jan 28, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants