I created a view to display total time from a date. So if I put in a date in the field it is displayed something like 2 months 5 day.

Is there a way to have it display in total days? for example: 62 days

I've been looking all over but cant seem to find anything. I'm sure I'm missing something.

Thanks

Comments

bazzly’s picture

Anyone know if there are any modules that can do this in views?

thanks

WorldFallz’s picture

There's 2 ways I can think of off the top of my head. One, just theme the field for the view and do the math there with PHP or two, use the views_php module. The second method easier but frowned upon (code in the ui), the first is a bit more complicated but probably best practice.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Thanks for the info! Seems like both are a bit over my head.
Silly question... I have read in multiple places to theme a field in/for a view.... This was the only info I found https://www.drupal.org/node/812014 still not sure how to go about it.. Any good links you might know of?

WorldFallz’s picture

the best info I've found was right in views advanced help-- there's a topic called 'theme information' that describes how to find the correct template, and then a link at the bottom of the page that has more info about how to use the template. I'd recommend starting there-- and post back if you have any more questions.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Starting to get somewhere... this helped me a bit
https://www.drupal.org/node/352970

Now to mess with code I guess.. Not sure how/what to do there..

bazzly’s picture

So I think I figured the template part out...but for the code, I'm not sure where I would start... Not sure if I'm even using the correct template.

So the template in use would be the field template its self correct..?
If that is correct... how would I go about starting the code?

Thanks again

WorldFallz’s picture

Yep, from what you've described so far I would recommend using the field level template. And if you only need this field themed this way in a particular view, I would use the view specific template for that field. Something like: views-view-field--myviewname--myfieldname.tpl.php (you find this from clicking the theme information link in views advanced settings fieldset).

As for the code, there's really not enough info to even speculate. Where does the date field come from? What type of field is it? How do you arrive at the '2 months 5 day' interval (a single date field is not an interval)?

The only direction I can give atm, is that since months don't have the same number of days, it's not as simple as (# months * 30) + # days. If you google around about doing this calculation with php you'll should find plenty of info. If not, provide more details and maybe I can make a better recommendation.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Thanks for the info again!

The date field is a Field type of date... (Using the date module)
They're just inputting one date, but I wasn't sure if it could calculate the days from the input date (the field they use to input) from the system date.
I'm not sure how views does it with the Formatter "Time ago", but was thinking this would be similar...
My guess is I'm thinking in the wrong direction on how this would work.

Thanks for the insight on the template... I thought the template names were just name suggestions. I couldn't see that the names served a function. I was looking for templates with page, block, or whatever in them..now that I think about it a bit more, makes sense.

WorldFallz’s picture

OK, if using the date module and a timestamp date field type, it actually becomes pretty simple.

  1. add the 'Content: Post date' field to your view if not already there (you can set the 'exclude' option if you don't want/need it to show in the view).
  2. add the date field to the view
  3. create the template file (be sure to click the 'rescan' button in views advanced settings or clear the cache)
  4. then dump the $row variable in the template-- it will contain the data in the view from that row you can use in your output.

The code you need should be something like (pseudo code):

<?php
$created
= $row->created;
$date = $row->{$field->field_alias};
echo
round(($date-$created)/86400, 1) . ' days';
?>

And just as an fyi, views probably uses format_interval which, afaik, doesn't have a way to force it to use days.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Oooohhh So close...
I dont think that will work ( I received errors anyway), because they're basically updating one node anytime something changes. Its a simple kpi. So anytime they update the node, that would also reset that field...correct?(the node has multiple fields that get updated)
For example see below:

Total Days Best Performance: 157
Total Days Without A Near Miss: 6
Last Lost Time Accident: February 5, 2014
Last Lost Time Accident Days: 206
Last OSHA Recordable: March 26, 2014
Last OSHA Recordable Days: 157

The spots where it's a number is a number field.
Any place there is a date the preferred output would be X days

All that is done by node update, there is no new content being created.
This make sense? I'm sure there is a better way of doing this.

Could you give a breakdown of the code so I can understand what it does? Sorry for asking so much....and thank you very much for your help!

If its easier to go by content post date I may try something else, but I would need to figure out how to theam a few fields.. Any ideas?

WorldFallz’s picture

they're basically updating one node anytime something changes.

doesn't seem like a good way to go about it-- you lose history. I do KPI type nodes all the time and I always create a new node for each 'reading'.

In anycase, you could always use the last updated time. If that's not acceptable, then you'll have to figure out what date you want to use. An interval requires a start and end point. The only ones available by default with a node is created and changed.

It's only 3 lines of pseudo code, but here's a commented version:

<?php
//get the last updated date of the node from the current $row of the view
$updated = $row->changed;

//get the value of the datefield from the current $row of the view
$date = $row->{$field->field_alias};

//calculate the number of days between the two dates rounded to the first decimal point.
//86400 = the # of seconds in 1 day
echo round(($date-$updated)/86400, 1) . ' days';
?>

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Thanks again! I get this error

Notice: Undefined property: stdClass::$changed in include() (line 3 of /var/www/safetytest/sites/all/themes/bartik_plus/views/views-view-field--non-recordables--field-last-osha-recordable.tpl.php).

So I guess it doesn't like "changed"?

I did change "created" to "updated" in the line below, because there were 2 errors. By changing that I now have one..
echo round(($date-$created)/86400, 1) . ' days';

So using the code before, in theory every time content is created it would do an automatic update to the kpi correct?

WorldFallz’s picture

There's no '$changed' in the pseudo code I provided above, but you would have had to add the "content update time" field to the view (also excluded if desired) in order to be able to use it (i should have been more explicit in my comment).

And keep in mind this is pseudo code -- i'm not 100% sure of the view data you'll see so you need to dump $row in your template file in order to see if $row->changed is correct.

And yep-- $created was a typo carried over from my copy and paste of the previous code (i've corrected my comment).

in theory every time content is created it would do an automatic update to the kpi correct

Not sure what you mean. This post is all about formatting a view so the view will be displayed properly, but nothing in the content is getting updated here.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

So trying it a different way... I added these fields to content that will be generated. So the first code example you showed me should work, but my question here is, how would I apply that to 3 different fields? Also, would it be possible to keep the dates, and show the total days next to or below?

For example

OSHA Recordable Date: September 1, 2014 (3 days)
Near Miss Date: August 16, 2014 (19 Days)
Lost Time Accident: September 11, 2014 ....or something like below.

OSHA Recordable Date: September 1, 2014 (3 days)
Last OSHA Recordable 3 days ago
Near Miss Date: August 16, 2014 (19 Days)
Last Near Miss Date: 19 Days ago

Thanks again.. this and been a good learning experience even though I have failed so far..

WorldFallz’s picture

Sorry, you've lost me here. I'm not sure what fields you've added to content-- all we're discussing in this post is formatting a the output of a views field.

how would I apply that to 3 different fields?

You would need to create similar templates for the other views fields you want to format (or perhaps use the row level template file instead).

would it be possible to keep the dates, and show the total days next to or below?

yep. You can either not exclude the original field you're using for the calculate or, if you want more control of the output, just format the output in your template file however you like.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Sorry, confusing people seems to be one of my better skills.. ;)

In my other post I stated all they do is update a node. I was thinking of having it.... so if something happens they could add it to a content type...(Create new content)
So this content is generated daily already, so in theory if I added those field to the content, and they enter info in one of those fields, it should update the kpi. If the field was not updated (Blank) then it would not change...correct?

So I need to create a template for each field then... I'm going to give it a go using your original code and see if I can get something to happen... I'm sure I'll be posting my mangled code soon lol

This is way over my head, but cool stuff!

bazzly’s picture

what does this tell me?
Notice: Undefined property: stdClass::$created in include() (line 2 of /var/www/safetytest/sites/all/themes/bartik_plus/views/views-view-field--block-1.tpl.php).
Line 2: $created = $row->created;

So I can tell something is undefined.. so I think its telling me "created" is undefined? If that is the problem, how would I define it?

$date = $row->{$field->field_alias};
where it says field _alias...would I enter the field name in that area of the code?

codecademy..here I come...

WorldFallz’s picture

This is why I suggested dumping (print_r/var_dump/dpm etc) $row in your template file. It will show you exactly what data you have to work with and what it's called. As I've already stated several times, i provided pseudo code-- not actual confirmed correct runnable code.

where it says field _alias...would I enter the field name in that area of the code?

no you use that as is-- it's views method of making sure your template always has the correct field-- even if the view itself changes.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

bazzly’s picture

Silly question...how does the output help?

Using this code I found to get info on the field..(dont know how to use $row

echo "<pre>";
print_r($row);
echo "</pre>";

no idea how this helps me...

Thanks

bazzly’s picture

Go it!!!

Thanks!

WorldFallz’s picture

excellent ;-)

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.