Skip to content

Commit

Permalink
Fix: use the date timestamp with the proper timezone on the donor das…
Browse files Browse the repository at this point in the history
…hboard (#7671)
  • Loading branch information
glaubersilva authored Jan 27, 2025
1 parent 2ce51ba commit 3fe9f4d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/DonorDashboards/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Give\DonorDashboards\Pipeline\Stages\UpdateDonorAvatar;
use Give\DonorDashboards\Pipeline\Stages\UpdateDonorCompany;
use Give\Donors\Models\Donor;
use Give\Framework\Support\Facades\DateTime\Temporal;

/**
* @since 2.10.0
Expand Down Expand Up @@ -79,6 +80,7 @@ public function update($data)
/**
* Return array of donor profile data
*
* @unreleased Replace strtotime() with Temporal::getDateTimestamp() to prevent the use of dates with wrong timezones
* @since 2.10.0
*
* @return array
Expand All @@ -97,11 +99,11 @@ public function getProfileData()
'lastName' => $this->donor->get_last_name(),
'emails' => $this->donor->emails,
'sinceLastDonation' => ! empty($this->donor->get_last_donation_date()) ? human_time_diff(
strtotime($this->donor->get_last_donation_date())
Temporal::getDateTimestamp($this->donor->get_last_donation_date())
) : '',
'avatarUrl' => $this->getAvatarUrl(),
'avatarId' => $this->getAvatarId(),
'sinceCreated' => human_time_diff(strtotime($this->donor->date_created)),
'sinceCreated' => human_time_diff(Temporal::getDateTimestamp($this->donor->date_created)),
'company' => $this->donor->get_company_name(),
'initials' => $this->donor->get_donor_initals(),
'titlePrefix' => $this->getTitlePrefix(),
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Support/Facades/DateTime/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Give\Framework\Support\Facades\DateTime;

use DateTime;
use DateTimeInterface;
use Give\Framework\Support\Facades\Facade;

Expand All @@ -14,6 +13,7 @@
* @method static string getFormattedDateTime(DateTimeInterface $dateTime)
* @method static string getCurrentFormattedDateForDatabase()
* @method static DateTimeInterface withoutMicroseconds(DateTimeInterface $dateTime)
* @method static int getDateTimestamp(string $date, string $timezone = '')
*/
class Temporal extends Facade
{
Expand Down
28 changes: 28 additions & 0 deletions src/Framework/Support/Facades/DateTime/TemporalFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use Give\Log\Log;

/**
* @since 2.19.6
Expand Down Expand Up @@ -76,4 +79,29 @@ public function withoutMicroseconds(DateTimeInterface $dateTime)

return $newDateTime;
}

/**
* @unreleased
*/
public function getDateTimestamp(string $date, string $timezone = ''): int
{
try {
$timezone = empty($timezone) ? wp_timezone_string() : $timezone;
$timezone = new DateTimeZone($timezone);
$date = new DateTime($date, $timezone);

return $date->getTimestamp();
} catch (Exception $e) {
Log::error(
'Failed to parse date string into a timestamp',
[
'input_date' => $date,
'input_timezone' => $timezone,
'error_code' => $e->getCode(),
'error_message' => $e->getMessage(),
]
);
return 0;
}
}
}

0 comments on commit 3fe9f4d

Please sign in to comment.