Bill Bronson
Bill Bronson

Reputation: 560

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?

Upvotes: 1

Views: 774

Answers (2)

JazZ
JazZ

Reputation: 4579

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.

Upvotes: 2

Abhay Maurya
Abhay Maurya

Reputation: 12277

I think you are trying to get something like this:

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

$time_compare = strtotime($dateofproduct);
$time_now = strtotime($actualdate);
$four_weeks = 4*7*24*60*60;
if($time_compare > ($time_now-$four_weeks)){
    //issue it NEW badge
}

I hope it helps

Upvotes: 0

Related Questions