Skip to content

PMPro_Member_Edit_Panel_Memberships.save incorrectly uses local timezone when saving membership start date #3422

@kk-sonata

Description

@kk-sonata

Describe the bug
When an admin edits a user membership, PMPro saves the membership start date to the database using the server's local timezone. WordPress's current_time function returns the local time unless the $gmt argument is specified.
'startdate' => current_time( 'mysql' )

// Add the membership level.
$level_to_add = array(
'user_id' => $user->ID,
'membership_id' => $level_id,
'code_id' => '',
'initial_payment' => 0,
'billing_amount' => 0,
'cycle_number' => 0,
'cycle_period' => 'month',
'billing_limit' => 0,
'trial_amount' => 0,
'trial_limit' => 0,
'startdate' => current_time( 'mysql' ),
'enddate' => empty( $expiration ) ? 'NULL' : date( 'Y-m-d H:i:s', strtotime( $expiration ) )
);

However, when retrieving the start date function pmpro_getMemberStartdate assumes that the startdate in the DB is UTC (+00:00), and applies a TZ conversion to it. This causes an inaccurate startdate to be returned.

if ( ! empty( $level_id ) ) {
$sqlQuery = "SELECT UNIX_TIMESTAMP(CONVERT_TZ(startdate, '+00:00', @@global.time_zone)) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND membership_id IN(" . (int) $level_id . ") AND user_id = '" . $user_id . "' ORDER BY id LIMIT 1";
} else {
$sqlQuery = "SELECT UNIX_TIMESTAMP(CONVERT_TZ(startdate, '+00:00', @@global.time_zone)) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND user_id = '" . esc_sql( $user_id ) . "' ORDER BY id LIMIT 1";
}

To Reproduce
Steps to reproduce the behavior:

  1. Edit a user's PMPro membership level through the Wordpress dashboard
  2. While logged in as the user, execute a snippet such as
	global $current_user;
	$startdate = pmpro_getMemberStartdate($current_user->ID);
	echo 'Membership start date: ' . date("Y-m-d H:i:s", $startdate) . '<br/>';
  1. Incorrect start date will be returned

Expected behavior
Dates should be saved in the database as UTC, not local timezone

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only Paid Memberships Pro plugin is active.
  • This bug happens with a default WordPress theme active, or Memberlite.
  • I can reproduce this bug consistently using the steps above.

WordPress Environment

Details Wordpress 6.8.1 PHP 8.2.28 PMPro 3.4.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions