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

Age Calculation

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #11  
Old 05-03-2010, 07:51 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Try using this:

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} {literal} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { {/literal} var calday = {$extra_fields.0.field_value}; var calmon = {$extra_fields.1.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} if(curday == "" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="") { alert("please fill all the values and click go -"); } else { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); document.birthday.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; var monleft = (dife[0]*12)+dife[1]; var secleft = diff/1000/60; var hrsleft = secleft/60; var daysleft = hrsleft/24; document.birthday.months.value=monleft+" Month since your birth"; document.birthday.daa.value=daysleft+" days since your birth"; document.birthday.hours.value=hrsleft+" hours since your birth"; document.birthday.min.value=secleft+" minutes since your birth"; var as = parseInt(calyear)+dife[0]+1; var diff = Date.UTC(as,calmon,calday,0,0,0) - Date.UTC(curyear,curmon,curday,0,0,0); var datee = diff/1000/60/60/24; document.birthday.nbday.value=datee+" days left for your next birthday"; } } $(document).ready(function(){ calage(); }); </script> {/literal} <form name="birthday"> <input name="age" size="40" value="Result"><br> </form>



It auto-calculates on page load, but it requires jquery (no worry, x-cart comes with it and will not have any issues :P) so importing into another program won't work later unless it uses jquery.
__________________
- Shane Munroe
Reply With Quote
  #12  
Old 05-04-2010, 05:49 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Not working for me, just showing the "Results".

http://littlepuppiesonline.msidesigns.com/product.php?productid=1&cat=28&page=1

I also tried scaling it down since we don't need some of the other calculations there were in the original form.

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} {literal} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { {/literal} var calday = {$extra_fields.0.field_value}; var calmon = {$extra_fields.1.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); document.birthday.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; var monleft = (dife[0]*12)+dife[1]; var secleft = diff/1000/60; var hrsleft = secleft/60; var daysleft = hrsleft/24; } $(document).ready(function(){ calage(); }); </script> {/literal} <form name="age"> <input name="age" size="40" value="Result"><br> </form>
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #13  
Old 05-04-2010, 05:51 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Do you think we'd better off storing the calculation results as a variable and displaying it like the rest of the site and not using the <form>?
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #14  
Old 05-04-2010, 02:34 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Thankgod firebug exists.
Enter numbers for their birthdays without 0's preceding such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 etc


You also stripped out too many {} brackets. Trying to find where you're missing one...



Edit:
Find this:
Code:
$(document).ready(function(){ calage(); });

On the line before it, add a }
__________________
- Shane Munroe
Reply With Quote
  #15  
Old 05-04-2010, 06:05 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Ok so I tried it this way. I compared the modified one against the original and made sure all of the { } were correct.

It's still not displaying the info.

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} {literal} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); var calday = {$extra_fields.0.field_value}; var calmon = {$extra_fields.1.field_value}; var calyear = {$extra_fields.2.field_value}; function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); document.birthday.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; } $(document).ready(function(){ calage(); }); </script> {/literal} <form name="age"> <input name="age" size="40" value="Result"><br> </form>
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #16  
Old 05-04-2010, 08:28 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

I see what you did.
You moved stuff around as well.

Notice my code:
Code:
{/literal} var calday = {$extra_fields.1.field_value}; var calmon = {$extra_fields.0.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} if(curday == "" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="")

I ended the opening literal statement right before using smarty, and then started again after it. This is due, as I said before, smarty using {} and javascript as well... They don't play nice together.

Your code keeps the smarty calls in literal
Code:
var calday = {$extra_fields.0.field_value}; var calmon = {$extra_fields.1.field_value}; var calyear = {$extra_fields.2.field_value};

if its taken literally, it will be. The actual values are those.

Another thing you did was change the form.

Code:
<form name="birthday"> <input name="age" size="40" value="Result"><br> </form>
That was the original.

Yours:
Code:
<form name="age"> <input name="age" size="40" value="Result"><br> </form>

Thing is, the script only outputs the variable to a form named birthday. You changed it to age, thus another issue.


In any case... The code...
Already stripped out some code, removed extra spaces, changed it so the form it outputs to is called age, changed form name to age and fixed literal placing. Tested and works...

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); var calday = {$extra_fields.1.field_value}; var calmon = {$extra_fields.0.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); document.age.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; } $(document).ready(function(){ calage(); }); {/literal} </script> <form name="age"> <input name="age" size="40" value="Result"><br> </form>
__________________
- Shane Munroe
Reply With Quote
  #17  
Old 05-04-2010, 08:56 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Thanks for all your help on this. I'm still only getting "Result" in the form box.

I did check the fields in the product. I still had 08 as the month, but that didn't make any difference when I changed it to 8
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #18  
Old 05-04-2010, 09:55 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Don't use 08 or 09. Javascript won't accept it as a number.


Edit:
Checked your site, going thru the javascript now to see what changed...

Edit2:
Found it.

Your site:
Code:
<input name="age" size="40" value="Result"><br>

Proper:
<form name="age">
<input name="age" size="40" value="Result"><br>
</form>
__________________
- Shane Munroe
Reply With Quote
  #19  
Old 05-05-2010, 12:44 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Quote:
Originally Posted by Tal
Don't use 08 or 09. Javascript won't accept it as a number.


Edit:
Checked your site, going thru the javascript now to see what changed...

Edit2:
Found it.

Your site:
Code:
<input name="age" size="40" value="Result"><br>

Proper:
<form name="age">
<input name="age" size="40" value="Result"><br>
</form>

Yeah that's what I had on there. I even check the source code. hmmm Maybe it's because of Firefox?

I did see in the code that the var's are being set to the correct day,month,year.

Edit:
Nope IE blows too. :P

OMG this is so stupid, why is it not working.

I was asking before, is there a different way to store the calc and display it not using the form?

Really what I'd like is the details page to look like is:
Puppy Name Junior is 4 months, 2 days old.
Other Sites Sell For: $699.00
Our price: $0.01
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #20  
Old 05-05-2010, 04:06 PM
  gb2world's Avatar 
gb2world gb2world is offline
 

X-Wizard
  
Join Date: May 2006
Location: Austin, TX
Posts: 1,970
 

Default Re: Age Calculation

Doing this in the php file would seem to be easier and more efficient - if you are comfortable modifying the php files. You can continue to use the extra fields to capture the birthdate. In php, you can use the time function to get the current date. You can use strtotime to turn your string date in the extra fields to a timestamp so you can do the the math. Then, you use regular math to format the difference in seconds into years/months/days. While it is not doing exactly what you want, if you understand the concepts in this thread - it helps to explain how the extra fields are accessible in the php, and how to pass the result to the templates.
__________________
X-CART (4.1.9,12/4.2.2-3/4.3.1-2/4.4.1-5)-Gold
(CDSEO, Altered-Cart On Sale, BCSE Preorder Backorder, QuickOrder, X-Payments, BCSE DPM Module)
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



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 02:27 PM.

   

 
X-Cart forums © 2001-2020