How to display end date in Woocommerce Subscriptions with a specific date format?

I just want to ask on how to display the end date (or next payment date) in Woocommerce Subscriptions in "MM/DD/YYYY" format?

I'm currently using WooCommerce, WooCommerce Subscriptions, WooCommerce Memberships and Elementor Pro (with Dynamic Content for Elementor plugin).

I was able to display the End date of the subscription, but I just don't know on how to display it in a specific date format.

This is the code I'm using:

$users_subscriptions = wcs_get_users_subscriptions($user_id);

foreach ($users_subscriptions as $subscription){
  if ($subscription->has_status(array('active'))) {

         echo $subscription->get_date('end'); 
  }
} 

The code displays the necessary date, but I need to display the date in "MM/DD/YYYY" format. As of now, Its displaying in "MM-DD-YYYY HH:MM:SS".

I hope anyone could help. Thanks!


You could convert $subscription->get_date('end') to the format that you need using dateDocs function.

So, replace this line:

echo $subscription->get_date('end'); // Returns "MM-DD-YYYY HH:MM:SS" (i.e Jan-21-2022 09:35:37)

With this line:

echo date('M/d/Y', strtotime($subscription->get_date('end'))); // Returns "MM/DD/YYYY" (i.e Jan/21/2022)

Or if you need to show it like 01/21/2022, then use the following:

echo date('m/d/Y', strtotime($subscription->get_date('end')));