Date Subtraction
3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 6, 2019 (RSS)
By KennyH - August 5, 2019
I am trying to add a stat counter that would take the year founded and subtract it from the current year then display the difference.
In the code below, I tried replacing
'-2003 year'
with
-$page_elementsRecord['year_founded'] year
and I get "syntax error, unexpected 'year' (T_STRING), expecting ',' or ')' in /home/public_html/stats.php on line 27."
<strong data-to="<?php $date = date('Y');
$newdate = strtotime ( -$page_elementsRecord['year_founded'] year , strtotime ( $date ) ) ;
$newdate = date ( 'Y' , $newdate );
echo $newdate; ?>" data-append="+">0</strong>
If I remove - year then it just displays the year.
By daniel - August 6, 2019
Hi KennyH,
The syntax error here is being caused by some issues with string formatting. Strings need to be within quotation marks (either single or double) and can be joined with variables using a period. The correct way to write this would be:
$newdate = strtotime ( "-" . $page_elementsRecord['year_founded'] . " year" , strtotime ( $date ) ) ;
This may give you the result you want, though if all you need is the difference between two years you could likely also simplify it and just use a subtraction, something like this:
<strong data-to="<?php $date = date('Y');
$newdate = $date - $page_elementsRecord['year_founded'];
echo $newdate; ?>" data-append="+">0</strong>
Let me know if you're able to fix the issue, or have any other questions!
Thanks,
Technical Lead
interactivetools.com