0

I am displaying webform submissions in a view, and need to add a "NEW" notification if the submission is less than a day old.

I have created views-view-field--myfield.tpl.php and have managed to change the value of my field using the template.

I am grabbing the submitted date, which I have printed already to make sure it works, and it comes up as 13/05/2014 - 23:13

I am then trying to figure out how to return a value for a date that is less than 1 day old. I'm pretty sure the following should do it, but it always renders "not new".

<?php
$submitted = $view->style_plugin->rendered_fields[$view->row_index]['submitted'];

if( $submitted > strtotime('-1 day') ) {
  echo '<div style="color:red;">NEW!</div>';
}
  else
{
  echo 'not new';
}
 ?>

Can anyone see where I'm going wrong?

UPDATE:

So, this works...

if( strtotime('14 May 2014') > strtotime('-1 day')) {
  echo '<div style="color:red;">NEW!</div>';
}
else
{
echo 'nothing <br />';
}

If i set it to 14th may (today) Its shows up as NEW! , any earlier, it prints nothing.

So, this leads me to believe there is a problem with my submitted date.

But, If I put echo $submitted; i get 13/05/2014 - 23:13

If I put $newDate = date("d M Y", strtotime($submitted)); echo $newDate; i get 01 Jan 1970 for every result.

And If I put $newDate = date("d M Y", $submitted); echo $newDate; I also get 01 Jan 1970 for every result.

So now I don't know what's going on. If its not in timestamp format, strtotime should sort it shouldnt it?

1 Answer 1

0

Probably need to strip the html from $submitted and convert to timestamp first, e.g something like:

$submitted = $view->style_plugin->rendered_fields[$view->row_index]['submitted'];

if (strtotime(strip_tags($submitted)) > strtotime('-1 day')) {
  echo '<div style="color:red;">NEW!</div>';
}
6
  • hmm. Nope not working either. I dont actually get any results when I echo strip_tags($submitted); , only when i echo $submitted.
    – Collins
    May 14, 2014 at 8:50
  • strip_tags($submitted); only strips html, the submitted text value should still be there - what value do you get for $submitted? May 14, 2014 at 9:55
  • echo $submitted; returns 13/05/2014 - 23:13
    – Collins
    May 14, 2014 at 10:30
  • I tried printing todays date and the exact same format, and doing a subtraction to get the difference, and then doing $difference / 84600 to get the days, but the result is always 0.
    – Collins
    May 14, 2014 at 10:31
  • echo strip_tags('13/05/2014 - 23:13'); returns 13/05/2014 - 23:13 you need to convert the value to a unix timestamp with strtotime to compare, as stated in my answer. May 14, 2014 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.