Bill Bronson Bill Bronson - 1 year ago 65
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download