Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
85 stars 31 forks source link

Fix function deprecation warning in PHP >= 8.1 #368

Closed davefx closed 1 year ago

davefx commented 1 year ago

Fixes #212

Description

This PR fixes the problem that causes a deprecation warning in PHP 8.1 due to the usage of function strptime.

As the method already includes a fallback for the cases where strptime is not available (like Windows), I've simply added a check so in PHP >= 8.1 we use the alternative implementation.

How to test this PR

Function "wcs_is_datetime_mysql_format()" should keep working as it does until now, but without generating a warning in PHP >= 8.1.

Product impact

james-allan commented 1 year ago

Thanks for submitting the PR @davefx.

@mattallan what are your thoughts on just getting rid of the reliance on strptime() all together. There's a couple of reasons why I think this is a good idea:

  1. It's currently not even available on some environments. The function block comments says it isn't available on PHP running on Windows servers.
  2. Its being deprecated in PHP 8.1.
  3. We already have an alternative which uses regex and converting it into a date to check the validity.

I've also been doing some research and there might even be better ways for us to check the format matches and is a valid date. eg

$date = DateTime::createFromFormat( $format, $date_time );
return $date && $date->format($format) == $date_time;
brucealdridge commented 1 year ago

I'm going to close this as #396 is a more complete fix and has now been merged.