Bill Bronson Bill Bronson - 24 days ago 6
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:

<?php
$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?

Answer

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')));
var_dump($prod_is_new);
// output -> bool(false)

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

Hope it helps.