Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

PHP Code in template to use date function?

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 11-04-2013, 05:18 AM
  jcorneli's Avatar 
jcorneli jcorneli is offline
 

Advanced Member
  
Join Date: Jul 2007
Posts: 74
 

Default 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>";
}
__________________
www.usapolicesupply.com
v4.6.3

Xcartmods reBoot Template
WebsiteCM CDSEO Pro SEO
BCS Multipe Upselling Links
Firetank Feed Manager

BCS Product Importer Updater Pro
BCS Drop Shipper Pro
BCS Quote Request Module
BCS Printable Online Catalog
Reply With Quote
  #2  
Old 11-04-2013, 06:49 AM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default 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
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote

The following 2 users thank totaltec for this useful post:
jcorneli (11-04-2013), qualiteam (11-04-2013)
  #3  
Old 11-10-2013, 05:12 PM
  jcorneli's Avatar 
jcorneli jcorneli is offline
 

Advanced Member
  
Join Date: Jul 2007
Posts: 74
 

Default 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}
__________________
www.usapolicesupply.com
v4.6.3

Xcartmods reBoot Template
WebsiteCM CDSEO Pro SEO
BCS Multipe Upselling Links
Firetank Feed Manager

BCS Product Importer Updater Pro
BCS Drop Shipper Pro
BCS Quote Request Module
BCS Printable Online Catalog
Reply With Quote
  #4  
Old 11-10-2013, 06:38 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default 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.
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote
  #5  
Old 11-11-2013, 11:12 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

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

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
Attached Files
File Type: txt smarty-date-tricks.txt (1.2 KB, 89 views)
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote

The following 2 users thank totaltec for this useful post:
qualiteam (11-12-2013), tony_sologubov (11-12-2013)
  #6  
Old 11-12-2013, 05:36 PM
  jcorneli's Avatar 
jcorneli jcorneli is offline
 

Advanced Member
  
Join Date: Jul 2007
Posts: 74
 

Default 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
__________________
www.usapolicesupply.com
v4.6.3

Xcartmods reBoot Template
WebsiteCM CDSEO Pro SEO
BCS Multipe Upselling Links
Firetank Feed Manager

BCS Product Importer Updater Pro
BCS Drop Shipper Pro
BCS Quote Request Module
BCS Printable Online Catalog
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

   

 
X-Cart forums © 2001-2020