2016-11-14 72 views
1

我想建立一個「新」的徽章,顯示在店鋪概述。 爲此,我需要產品陣列中產品的日期。如何將產品的日期比實際日期和次數(WooCommerce/WordPress的)

例如:

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

我該怎麼辦,如:

當日期少於4週年長從現在開始,它獲得了「NEW」徽章?

+4

谷歌不在你的國家工作? –

+0

到目前爲止您嘗試了什麼? – JazZ

+0

通過current_time('Y-m-d')得到了實際的日期;但不知道如何比較它,因爲他們有其他格式。當前時間帶來「2016-11-14」,並得到日期給我一個格式,如「14.十一月2016」 –

回答

2

您可以使用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 
    ... 
} 

希望它可以幫助。

0

我認爲你試圖讓這樣的事情:

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

我希望它能幫助