Page 1 of 2
Age based on minutes
Posted: 03 Nov 2018 11:03
by Bart
I use this script to show the age of the files in the custom column.
$diffH = datediff(property("#date.m", <cc_item>), , "h");
return regexreplace($diffH / 24, "[.,]\d+$") . " days";
But it does not differentiate between files within one day/hour. How to add the minutes factor?
Bart
Re: Age based on minutes
Posted: 03 Nov 2018 11:16
by highend
But it does not differentiate between files within one day/hour. How to add the minutes factor?
Real world example?
Otherwise (wild guess):
Code: Select all
$diffH = datediff(property("#date.m", <cc_item>), , "n");
return regexreplace($diffH / (24 * 60), "[.,]\d+$") . " days";
Re: Age based on minutes
Posted: 03 Nov 2018 11:29
by jupe
Another wild guess, if you mean you want hours (minutes?) displayed up until it is 24 hours old, something like this might work:
Code: Select all
$diffH = datediff(property("#date.m", <cc_item>), , "h");
if $diffH <= 24 { return $diffH . " hours";} else {
return regexreplace($diffH / 24, "[.,]\d+$") . " days";}
Re: Age based on minutes
Posted: 03 Nov 2018 11:43
by Bart
What I mean is if there are a few files from the same day and I want to set them is the right order of age, the script reads the date and not till minutes. I want to list the files in age, even when there are a few minutes difference in modification date between the files.
Re: Age based on minutes
Posted: 03 Nov 2018 15:28
by RalphM
Then sort the files by the regular date modified column and have the age column just sit there as info.
Re: Age based on minutes
Posted: 03 Nov 2018 15:58
by Bart
Yes, a logic that did not come into my mind, but workable indeed.
Re: Age based on minutes
Posted: 03 Nov 2018 16:06
by Bart
Following the script from jupe, I try to make the following:
File age is mentioned in minutes if less than an hour
File age is mentioned in hours if less than an day
File age is mentioned in days for any other age
Something like this, but I failed to have it working:
$diffH = datediff(property("#date.m", <cc_item>), , "h");
if $diffH <= 24 { return $diffH . " hours";} else
if $diffH <= 1 regexreplace($diffH * 60, "[.,]\d+$") . " minutes";}
else
{
return regexreplace($diffH / 24, "[.,]\d+$") . " days";}
Re: Age based on minutes
Posted: 03 Nov 2018 16:35
by klownboy
I'm not sure if you had a unintentional return in the above when posting, but you can only have one "else" block and it must be last block in the control structure. Your first use of else should be "elseif" with the condition following it. I haven't looked at the rest of the code.
Code: Select all
elseif ($diffH <= 1) {regexreplace($diffH * 60, "[.,]\d+$") . " minutes";} //bracket your conditions for clarity
else ...
Re: Age based on minutes
Posted: 03 Nov 2018 17:09
by Bart
Noted. I can't make it working as I am not strong in programming.
Re: Age based on minutes
Posted: 03 Nov 2018 20:03
by klownboy
Hi Bart, I think it's best, since you want a minutes return if less than an hour, to find the SC datedif in minutes first instead of hours and work up from there.
Code: Select all
$diffH = datediff(property("#date.m", <cc_item>), , "n");
if ($diffH <= 60) { //change to "<" instead of "<=" for 60 min to show as 1 hour instead of 60 minutes
return $diffH . " minutes";}
elseif ($diffH <= 1440) {
return regexreplace($diffH / 60, "[.,]\d+$") . " hours";}
else {
return regexreplace($diffH / 1440, "[.,]\d+$") . " days";}
Re: Age based on minutes
Posted: 03 Nov 2018 20:35
by highend
This wouldn't work, Ken. At least if I understand it correctly. He wants a correct sort order even if two files have only a difference in minutes but are e.g. both 1 day old.
So the outputformat must be something like: "days.hours.minutes" to support this...
Re: Age based on minutes
Posted: 03 Nov 2018 20:54
by klownboy
Hi highend, obviously I wasn't too sure of what Bart wanted. The above will give him the proper return for each file in minutes, hours, or days. So he'd have to reverse the order and sort it if his goal is to sort based on that particular column. As RalphM pointed out though, he can sort it based on a normal date column and show these returns in a independent column separately.
Re: Age based on minutes
Posted: 04 Nov 2018 03:00
by RalphM
highend wrote: ↑03 Nov 2018 20:35So the outputformat must be something like: "days.hours.minutes" to support this...
With an ugly side effect that you would get/need a "00000 days 00 hours" for every file changed less than an hour ago, the double 00 and such probably being needed to make the sort really work, otherwise 10000, 1000, 100, 10, 1 days would all be sorted next to each other.
But it is Bart's decision which approach is preferred.
Re: Age based on minutes
Posted: 04 Nov 2018 05:37
by Bart
Thank you for helping me out, I started to learn how it works. I used the given solution and added some code for single/plural time periods and weeks.
Code: Select all
$diffH = datediff(property("#date.m", <cc_item>), , "n");
if ($diffH < 1) {
return $diffH . " minutes";}
elseif ($diffH < 2) {
return $diffH . " minute";}
elseif ($diffH < 60) {
return $diffH . " minutes";}
elseif ($diffH < 120) {
return regexreplace($diffH / 60, "[.,]\d+$") . " hour";}
elseif ($diffH < 1440) {
return regexreplace($diffH / 60, "[.,]\d+$") . " hours";}
elseif ($diffH < 2880) {
return regexreplace($diffH / 1440, "[.,]\d+$") . " day";}
elseif ($diffH < 10080) {
return regexreplace($diffH / 1440, "[.,]\d+$") . " days";}
elseif ($diffH < 20160) {
return regexreplace($diffH / 10080, "[.,]\d+$") . " week";}
else {
return regexreplace($diffH / 10080, "[.,]\d+$") . " weeks";}
Re: Age based on minutes
Posted: 04 Nov 2018 08:23
by highend
Why not like this?
Code: Select all
$diffM = datediff(property("#date.m", <cc_item>), , "n");
if ($diffM < 60) { $time = $diffM . " minute(s)"; }
elseif ($diffM < 1440) { $time = ($diffM / 60) . " hour(s)"; }
elseif ($diffM < 10080) { $time = ($diffM / 1440) . " day(s)"; }
else { $time = ($diffM / 10080) . " week(s)"; }
return regexreplace($time, "[.,]\d+");
Less repetition and the brain is normally able to get the correct context out of e.g. minute(s) by looking at the value