Magento 1 - How to check if a product is new
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 && !$newsToDate) {
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/
There's no single method call. At least I'm not aware of it.
Nevertheless you can always use something like this:
<?php if (date("Y-m-d") >= substr($_product->getNewsFromDate(), 0, 10) && date("Y-m-d") <= substr($_product->getNewsToDate(), 0, 10)) : ?>
...
<?php endif ?>
Sorry I'm not good in PHP date comparison.
Good question by the way.
For reference, here's the helper method that I came up with to check for newness. It supports both the Featured dropdown as well as the dates, and supports either date being empty as well.
public function isNew($product)
{
if ($product->getData('featured_product')) {
return true;
}
if ($product->getData('news_from_date') == null && $product->getData('news_to_date') == null) {
return false;
}
if ($product->getData('news_from_date') !== null) {
if (date('Y-m-d', strtotime($product->getData('news_from_date'))) > date('Y-m-d', time())) {
return false;
}
}
if ($product->getData('news_to_date') !== null) {
if (date('Y-m-d', strtotime($product->getData('news_to_date'))) < date('Y-m-d', time())) {
return false;
}
}
return true;
}
UPDATE: Thanks to @Rooooomine for mentioning that this manual date conversion I suggested is a very bad idea due to potential locale issues. Check out Mage::app()->getLocale()->isStoreDateInInterval($product->getStoreId(), $newsFromDate, $newsToDate)
instead.