X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   PHP Code in template to use date function? (https://forum.x-cart.com/showthread.php?t=68078)

jcorneli 11-04-2013 05:18 AM

PHP Code in template to use date function?
 
In a template is it possible to replace the code like the following examples to use a date function:

<li>
<a href="https://www.usapolicesupply.com/help.php?"><img src="{$AltSkinDir}/custom/welcome/promotions/image_slider/images/operator.jpg" alt="" /></a>
<p class="flex-caption">We have access to over 120,000 products.</p>
</li>

<li>
<a href="http://www.usapolicesupply.com/breast-cancer-awareness/"><img src="{$AltSkinDir}/custom/welcome/promotions/image_slider/images/bcaware.jpg" alt="" /></a>
<p class="flex-caption">October is Breast Cancer Awareness Month</p>
</li>

with PHP code like this:

// Macmillan Cancertalk week (21-25 Jan)
if ((date('m') == 01) && (date('d') >= 21) || (date('m') == 01) && (date('d') <= 23)) {
echo "<img src=\"images/ribbons/cancertalk.gif\" height=\"145\" width=\"175\" alt=\"Macmillan Cancertalk\" /><br /><h6 class=\"awareness\">Macmillan Cancertalk Week <span class=\"morelink\"><a href=\"the-bookstall-cancer-links-and-resources.php\">more...</a></span></h6>";
}
// Macmillan Cancertalk week (21-25 Jan) and Cervical Cancer Awareness Week (24-30 Jan)
else if ((date('m') == 01) && (date('d') == 24)) {
echo "<img src=\"images/ribbons/macmillan_cervical.gif\" height=\"145\" width=\"175\" alt=\"Macmillan Cancertalk and white and teal awareness ribbons\" /><br /><h6 class=\"awareness\">Macmillan Cancertalk Week &amp; Cervical Cancer Awareness Week <span class=\"morelink\"><a href=\"the-bookstall-cancer-links-and-resources.php\">more...</a></span></h6>";
}

totaltec 11-04-2013 06:49 AM

Re: PHP Code in template to use date function?
 
Yes, but you want to use smarty code in your template.

http://www.smarty.net/docsv2/en/language.modifier.date.format.tpl

Examples:
Code:

<p>{$smarty.now|date_format:$config.Appearance.date_format}</p>
<p>{$smarty.now|date_format:"%A"}</p>
<p>{$smarty.now|date_format:"%B"}</p>
<p>{$smarty.now|date_format:"%Y"}</p>

Output:
11-04-2013

Monday

November

2013

jcorneli 11-10-2013 05:12 PM

Re: PHP Code in template to use date function?
 
Am I going in the right direct with this?

{if $smarty.now|date_format:%A eq 'Dec'}
Welcome Sir.
{elseif $smarty.now|date_format:%A <> 'Dec'}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}

totaltec 11-10-2013 06:38 PM

Re: PHP Code in template to use date function?
 
Quote:

Originally Posted by jcorneli
{elseif $smarty.now|date_format:%A <> 'Dec'}

There are a few things horribly wrong with this snippet of code. First off, you can't use mathematical comparison operators on strings. You would only use less than or greater than comparisons on integers (numbers).

Second, you don't usually make two comparisons at once. You have less than/greater than, it would need to be is variable less than or is variable greater than.

I don;t see why you need the else if. Just say If = Dec do this else do this. If it is not december the else would fire.

totaltec 11-11-2013 11:12 PM

Re: PHP Code in template to use date function?
 
1 Attachment(s)
Here we go, tried to make it easy and spelled out for everyone to grasp completely. I also attached the code in a text file so you could save it and reference later easily.
Code:

<p>What is today's date smarty? <b>{$smarty.now|date_format:$config.Appearance.date_format}</b></p>

<p>What month is it smarty? <b>{$smarty.now|date_format:"%B"}</b></p>

<p>What day of the month is it smarty? <b>{$smarty.now|date_format:"%e"}</b></p>

<p>What day of the week is it smarty? <b>{$smarty.now|date_format:"%A"}</b></p>

<p>What year is it smarty? <b>{$smarty.now|date_format:"%Y"}</b></p>

<p>Is it November smarty?  <b>{if $smarty.now|date_format:"%B" eq "November"}Yes{else}No{/if}</b></p>

<p>Is it December smarty? <b>{if $smarty.now|date_format:"%B" eq "December"}Yes{else}No{/if}</b></p>

<p>Is it November 1st-9th smarty?  <b>{if $smarty.now|date_format:"%B" eq "November" and $smarty.now|date_format:"%e" gte 1 and  $smarty.now|date_format:"%e" lte 9}Yes{else}No{/if}</b></p>

<p>Is it November 10th-20th smarty?  <b>{if $smarty.now|date_format:"%B" eq "November" and $smarty.now|date_format:"%e" gte 10 and  $smarty.now|date_format:"%e" lte 20}Yes{else}No{/if}</b></p>

<p>Is it December 2nd-10th smarty?  <b>{if $smarty.now|date_format:"%B" eq "December" and $smarty.now|date_format:"%e" gte 2 and  $smarty.now|date_format:"%e" lte 10}Yes{else}No{/if}</b></p>

Output:
What is today's date smarty? 11-12-2013

What month is it smarty? November

What day of the month is it smarty? 12

What day of the week is it smarty? Tuesday

What year is it smarty? 2013

Is it November smarty? Yes

Is it December smarty? No

Is it November 1st-9th smarty? No

Is it November 10th-20th smarty? Yes

Is it December 2nd-10th smarty? No

jcorneli 11-12-2013 05:36 PM

Re: PHP Code in template to use date function?
 
Thank you totaltec (Mike)

I tried what you sent and it worked great and I now understand the concept.

Thanks

JEC


All times are GMT -8. The time now is 06:55 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.