Bill Bronson Bill Bronson - 7 months ago 31
PHP Question

How to compare the date of a product to actual date and count (WooCommerce/Wordpress)

I wanna to build a "NEW" badge that displays in the shop overview.
For that thing i need the date of a product from the product array.

for Example:

$dateofproduct = get_the_date('Y-m-d');
$actualdate = current_time('Y-m-d');

How can i do like:

When date is less than 4 weeks older from now on it gets the "NEW" Badge?


You could use the dateTime php object :

$prod_date = "05 September 2016";
$date_object = new DateTime();
$date_object->modify('-4 weeks');
$prod_is_new = (strtotime($prod_date) > strtotime($date_object->format('Y-m-d')));
// output -> bool(false)

// and now : 
if ($prod_is_new) {
    // Stuff to add the new badge

Hope it helps.