如何检查Magento中的产品是否是新产品?

How to check if a product is new in Magento 1.9.0.1?

I tried:

    <?php if($_product->getNewProduct()) { ?>
          ...my HTML code...
    <?php } ?>

but it dosn't work. What's wrong with it?

I solved in this way:

    <?php
        $current_date = Mage::getModel('core/date')->date('Y-m-d');
        $from_date = substr($_product->getNewsFromDate(), 0, 10);
        $to_date = substr($_product->getNewsToDate(), 0, 10); 

        $new = ($current_date >= $from_date && $current_date <= $to_date) || ($from_date == '' && $current_date <= $to_date && $to_date != '') || ($from_date != '' && $current_date >= $from_date && $to_date == '') ;
    ?>

    <?php if($new == 1) { ?>
             <img src="...
    <?php } ?>

Unfortunately, you cannot do this as that simple. There is no method exists for checking whether a product new.

The block class that is defined for new product is Mage_Catalog_Block_Product_New and is defined at app\code\core\Mage\Catalog\Block\Product\New.php. If you make a look there, then you will understand that, there is no good method that will be useful for your use.

The main method that is defined there is new products collection method. If you take a look on that method, ie on _getProductCollection(), you can see that it uses a start date and end dates (that is we setting for each product) to make the collection. So the best way that you can achieve is, check the product start date and end date and it comes inside our desired date, then execute codes inside your if statement. It will some what looks like this.

<?php 
    if (date("Y-m-d") >= substr($_product->getNewsFromDate(), 0, 10)         
        && date("Y-m-d") <= substr($_product->getNewsToDate(), 0, 10)) {
        //your code comes here
    }
?>

For more info, you can use this thread

In simply i want to say that a product is new depends on attribute by news_from_date and news_to_date

$todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);


 $collection = Mage::getResourceModel('catalog/product_collection');
  $collection 
            ->addAttributeToFilter('news_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter(
                array(
                    array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                    )
              )
               ->addAttributeToFilter('entity_id',$_product->getId())
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize(1)
            ->setCurPage(1);    
    if($count($collection)>0){
        //new products
    }

You absolutely must NOT convert and validate dates manually, because this can cause timezone issues. Magento has built-in method isStoreDateInInterval() in Mage_Core_Model_Locale class. So you should create helper method which can look like this

function isProductNew(Mage_Catalog_Model_Product $product)
{
    $newsFromDate = $product->getNewsFromDate();
    $newsToDate   = $product->getNewsToDate();
    if (!$newsFromDate && !$newToDate) {
        return false;
    }
    return Mage::app()->getLocale()
            ->isStoreDateInInterval($product->getStoreId(), $newsFromDate, $newsToDate);
}

More details you can find here http://quicktips.ru/all/check-product-is-new/

<?php 

$current_date=M age::getModel( 'core/date')->date('Y-m-d'); 
$from_date = substr($_product->getNewsFromDate(), 0, 10); 
$to_date = substr($_product->getNewsToDate(), 0, 10); 
$new =  ($current_date >= $from_date && $current_date <=$ to_date) || 
        ($from_date=='' && $current_date <=$ to_date && $to_date !='' ) || 
        ($from_date !='' && $current_date>= $from_date && $to_date == '') ; 
        //echo $new; ?>

<?php if($new==1 ) { ?>
<label for="">
    <?php echo $this->__('New') ?></label>
<?php } ?>

The above code has 3 cases:

  1. from_date <= current time <= to_date is true --> $new is true
  2. from date not set and current time <= to_date is true --> $new is true
  3. from_date <= current time and to_date not set is true --> $new is true