2016-11-14 1 views
1

ショップの概要に表示される「NEW」バッジを作成したいと考えています。 そのことについては、私は製品の配列から製品の日付が必要です。例のため商品の日付と実際の日付とカウントを比較する方法(WooCommerce/Wordpress)

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

どのように私は好きで行うことができます。

を日付が4週間未満古い場合は、今からそれが「NEW」バッジを取得しに?

+4

Googleがあなたの国で働いていませんか? –

+0

これまでに何を試しましたか? – JazZ

+0

は、current_time( 'Y-m-d')で実際の日付を取得しました。彼らは他のフォーマットを持っているので、それを比較する方法を知らない。現在の時刻は「2016-11-14」となり、日付は「2014年11月14日」のように表示されます –

答えて

2

に役立ちます願っています:

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

私はあなたがdateTime PHPのオブジェクトを使用することができ、それは

関連する問題