如何将产品日期与实际日期进行比较并计数 (WooCommerce/Wordpress)

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

我想制作一个显示在商店概览中的 "NEW" 徽章。 为此,我需要产品数组中产品的日期。

例如:

<?php 
$dateofproduct = get_the_date('Y-m-d'); 
$actualdate = current_time('Y-m-d');
?>

我该怎么做:

从现在开始,当日期小于 4 周时,它会获得 "NEW" 徽章?

我认为你正在尝试得到这样的东西:

$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
}

希望对你有帮助

您可以使用 dateTime php 对象:

$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
    ...
}

希望对您有所帮助。