1

I'm having trouble with the Global: Math Expression in a View. I have an entity type which is a student. I'd like the student grade level to change automatically from year to year, so I'm storing a graduation year and would like to display grade level by a Math expression. To do so, I need to include a current time element (month and year). I have read that time() is useable, but I can't get it to work. Perhaps I'm using the wrong format in the Expression box under Configure field? As a test, I put in just time(), but I get a zero at the output. Is time() useable within Views Math Expressions or do I have to abandon that approach and learn to use the Computed Field module? If time() is useable, would I be able to convert seconds to years by getting an integer value for time()/60/60/24/365.25 or does is there no integer conversion function here?

0

If you are using the Views PHP Field, http://drupal.org/project/views_php, you can enter in custom php field information. I would take the original date variable and subtract the date as of now to get the value you want:

  //Just creating a random freshman grad date

  $graduation_year = (date('Y-m-d', strtotime('+4 year')));

  $now = (date ('Y-m-d', (strtotime('now'))));

  $diff = abs($graduation_year - $now);

Based on the absolute difference in years you can do validation in your views field that validates this information and sets the student as a freshman, sophmore, junior or senior (assuming highschool :) ) based on the $diff value of 1,2,3,4.

That validation would look like:

if ($diff == 1) {
print('Senior');
}
elseif($diff == 2){
print('Junior');
}
elseif($diff == 3){
print('Sophmore');
}
elseif($diff == 4){
print('Freshman');
}

I didn't put exception handling but you get the idea.

  • I was hoping to just use the built in Global: Math Expression rather than adding another module, but I'll do it that way if there isn't a method to simply put in an expression with the time() function. I suppose being able to use php in Views could be pretty handy in other instances as well. Thanks. – Dan Oct 1 '12 at 13:06
  • Incidentally, Views PHP evidently doesn't pull the field value correctly through $row. Found the workaround here: drupal.org/node/1516348 – Dan Oct 1 '12 at 14:10
  • If this idea works for you please select it as the answer – generalconsensus Oct 1 '12 at 16:41
  • It works as a workaround, but presents a new problem, since that module has some bugs and is still in dev (was able to get around one bug, but not the second: click to sort doesn't seem to work properly). It's a partial solution for now. – Dan Oct 1 '12 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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