Forums

CCv6 Table cell text conditional formatting based on date Today

Elita- 13 Mar, 2018
Hello to all.
​
I am trying to implement following code somewhere in the CCv6 Connection:
​
$today = date("d.m.Y");
$polTerm = $this->data['polTerm'];
if ($polTerm < $today) {
echo '<td style="color: red;">$polTerm </td>';
}
where date saved in DB Column 'polTerm' and Read in the CCv6 Table column turns red if it is earlier than today,
but did not succeed because of several possible reasons:
​
1. Code is completely wrong πŸ˜€ ;
2. Place in CCv6 where this code is executed is completely wrongπŸ˜€
​
I tried two ways:
1. implement this code with Read Data function in Where conditions , but got the following error: Unknown column 'Submission.<td style="color' in 'where clause';
2. Create PHP function and then call it in index before Table view.
​
None is working.
​
I would be very happy if someone helped me with this.
​
In addition, what would be code if I wanted to create filter with this conditon?
​
Thank you in advance -
Elita
Max_admin 15 Mar, 2018
Hi Elita,
​
Assuming your PHP function name is php, you can call {fn:php} in the table columns views, and use this code in the PHP function:
$today = date("m.d.Y");
$polTerm = $this->data['polTerm'];
if ($polTerm < $today) {
echo '<div style="color: red;">'.$polTerm.'</div>';
}else{
echo $polTerm;
}
Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 16 Mar, 2018
Hello, Max, thank you for your help; The good thing - I got this code executedπŸ˜€ The bad thing - it got parse error:
​
Parse error: syntax error, unexpected (T_STRING) in your code on line  2: 
$polTerm = $this->data['polTerm'];
Could you tell whats wrong here - I'm bad with php, sorry.
​
Got this code passed php code syntax check for php-7.1.4:
​
$polterm = $this->get('submissions_list.row.Submission.polTerm');
$today = $date = JFactory::getDate();
if($today > $polterm){
<div style="color: red;"><?php echo $polterm ?></div>
}else{
echo $polterm;
}
but it still generates an error in CCv6: 0syntax error, unexpected '<' - do you have an idea why, Max?
Max_admin 16 Mar, 2018
Hi Elita,
​
The 2nd piece of code is wrong, the 4th line should be like this:
echo '<div style="color: red;">'.$polterm.'</div>';
The first code, where is line 2 ?
​
Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 17 Mar, 2018
This was line 2
$polTerm = $this->data['polTerm'];
So I tried to rewrite it and found somewhere this:
$polterm = $this->get('submissions_list.row.Submission.polTerm');
Do not know if this is right though.
​
This was the previous code version that gave that parse error:
$today = date("d.m.Y");
$polTerm = $this->data['polTerm'];
if ($polTerm < $today) {
echo '<div style="color: red;">'.$polTerm.'</div>';}
else{
echo $polTerm;}
Elita- 17 Mar, 2018
Tried this code below and got 0 syntax error, unexpected '' (T_STRING), (php-7.1.4)
$polterm = $this->get('submissions_list.row.Submission.polTerm');
$today = $date = JFactory::getDate();
if($today > $polterm){
echo '<div style="color: red;">'.$polterm.'</div>';
}else{
echo $polterm;
}
Max_admin 19 Mar, 2018
1 Likes
Hi Elita,
​
Pay attention not to copy the code from the forum here as there are some hidden characters which cause a PHP error, try the write the code yourself until I can fix this problem here.
​
Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 19 Mar, 2018
Function is working, however one problem appeared:
[polTerm] field contains data that is saved with CF6 calendar and formatted as follows:
​

​
And it seems not to be the correct date format. Question is - how to convert the field data so it could be properly compared to today date?
Max_admin 22 Mar, 2018
Hi Elita,
​
The "Real format" is the format under which the date value is saved and it should match the format of the read date value, under which format you store your date ? if it does not match the "real format" then you will need to change it using PHP before the "Display section" action.
​
Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 22 Mar, 2018
What confusing me Max, (sorry im just learning PHP), is that DD.MM.YYYY does not to seem to be right format PHP would understand -
So probably this DD.MM.YYYY has to be somehow formatted for PHP to read that properly as Date?
the numbers that are stored in the DB field are as following example: 02.01.2018
Max_admin 24 Mar, 2018
Hi Elita,
​
If the dates are stored in the database as 02.0.1.2018 and your "Real format" is DD.MM.YYYY then no formatting should be made, you can get the timestamp of this date using the PHP function "strtotime", then do the comparison to today's date in PHP too: strtotime(date("d.m.y"))
​
Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 24 Mar, 2018
Answer
1 Likes
I got this code working:
$polterm = $this->get('submissions_list.row.Submission.polTerm');
$endDate = new DateTime($polterm);
$curdate = new DateTime(date('d.m.Y'));

if($curdate > $endDate){
echo '<div style="color: red;">'.$polterm.'</div>';
}else{
echo $polterm;
}
To resume, for everybody who wants to format table cells based on Current date, do following:
​
1. Create php function with the similar code written above;
2. Add this function to Table Columns views:
Model.fieldname:{fn:php_function}
This topic is locked and no more replies can be posted.