How to make promos with formated date

Today I was lucky enough to do the promos for one of client sites, not just an image with the text, but with the need to specify the date of the event in label with a certain text. That's what I want to tell you about.

First I created a new page type, and then made 3 pages using this type. For two of them I added meta key date_end and dates in the past and the future. Fields with keys date_end and date_start automatically loads datepicker, so do not even have to be configured.

Next I put iblock pages with custom template based on template grid in the root page of promos. This template allows you to display items in a grid. I will not show you all the code of template, but only the most difficult part:

foreach ($result as $item){
	$class = '';
	if (isset($item['date_end'])){
		$until = strtotime($item['date_end']);
		if ($until > time()){
			$promo_date = 'till ' . tDate($this->locale, 'j F', $until);
		} else {
			$promo_date = 'finished';
			$class = 'expired';
	} else {
		$promo_date = 'termless';
	<div class="item <?=$class;?>">
		<div class="until"><?=$promo_date;?></div>

As you can see, even here nothing complicated. Just check the availability of meta and based on a comparison with the current date apply a necessary decision. Note that to display localized date, we use a special function tDate(), which is identical to the standard date() except for the first argument, in which the current site locale is transferred, and also translate the names of months and weeks to the selected language.

In order not to lose the "endless" promos from sight, you can apply the "Sticky" status to them. Then they will always be displayed before the promos with an expiration date.

© Arthur Grand, 2011–2023
Powered by SyDES