<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://sugarclub.sugarcrm.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/explore/help-forums/enterprise-professional/f/enterprise-professional-questions/2410/how-to-calculate-duration-between-two-date-time-fields</link><description>I have two date/time fields (both will always be in the past) from which I need to calculate a duration. (i.e. the difference between one and the other.) Ideally, I could display it as hours:minutes, but I&amp;#39;ll settle for just hours if I have to. I tried</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13790?ContentTypeID=1</link><pubDate>Mon, 25 Jul 2016 14:17:09 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:53cf0fe4-cfd4-40ba-b64c-9e33232ea8c4</guid><dc:creator>Adnan Cinar</dc:creator><description>&lt;p&gt;Hello All, &lt;/p&gt;&lt;p&gt;We have used following calculation as a whole for minutes&lt;/p&gt;&lt;p&gt;&amp;quot;multiply(subtract(multiply(subtract(divide(subtract(number(timestamp($chat_endtime1_c)),number(timestamp($chat_starttime1_c))),86400),floor(divide(subtract(number(timestamp($chat_endtime1_c)),number(timestamp($chat_starttime1_c))),86400))),24),floor(multiply(subtract(divide(subtract(number(timestamp($chat_endtime1_c)),number(timestamp($chat_starttime1_c))),86400),floor(divide(subtract(number(timestamp($chat_endtime1_c)),number(timestamp($chat_starttime1_c))),86400))),24))),60)&amp;quot; &lt;/p&gt;&lt;p&gt;From this link: &lt;a href="https://web.sugarcrm.com/support/issues/71341119-a7b5-1ec0-459c-539755b46fb0" rel="nofollow" target="_blank"&gt;SugarCRM, Inc.&lt;/a&gt; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13791?ContentTypeID=1</link><pubDate>Thu, 25 Feb 2016 18:01:30 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:fb4f2917-858a-490d-87da-9c2ad507542f</guid><dc:creator>Robert Bers</dc:creator><description>&lt;p&gt;Hi Joseph,&lt;/p&gt;&lt;p&gt;Have you tried something like this, (where 86400 is the number of seconds in a day):&lt;/p&gt;&lt;p&gt;&lt;strong&gt;round(divide(subtract(number(timestamp($date_modified)), number(timestamp($date_entered))),86400),0)&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Which gives you the number of days between 2 dates.&amp;nbsp; But of course the formula could be modified&lt;/p&gt;&lt;p&gt;in ways to produce the proper format you are looking for.&lt;/p&gt;&lt;p&gt;Bob Bers&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13789?ContentTypeID=1</link><pubDate>Wed, 24 Feb 2016 13:26:49 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:ba7fabad-e58e-4a78-bcaa-22eb4abdd5f8</guid><dc:creator>Liam Hastings</dc:creator><description>&lt;p&gt;In Sugar 7 there is a feature called &amp;#39;Recalculate Values&amp;#39; which will go through each selected option and update all calculated fields, allowing you to update 1000 records at a time maximum. If you have more than this many records then you can either do this in a few batches, or by exporting the data from your required module and then performing an import and update.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13788?ContentTypeID=1</link><pubDate>Wed, 24 Feb 2016 13:16:40 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:9f3961ad-ab85-40a6-9a87-29332ca859c6</guid><dc:creator>Joseph Moore</dc:creator><description>&lt;p&gt;Thanks, Liam.&lt;/p&gt;&lt;p&gt;I realized that the calculation does work as you describe. My problem was/is that the calculated field is not computed for existing records.&lt;/p&gt;&lt;p&gt;Is there a way to force Sugar to update all of my existing records? (UPDATE: Sorry, I see that I can update small batches through the list view, but will need to write a script to update all of the records.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13787?ContentTypeID=1</link><pubDate>Wed, 24 Feb 2016 08:38:27 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:13792add-e3ad-41fc-8327-39acefa97ca5</guid><dc:creator>Liam Hastings</dc:creator><description>&lt;p&gt;&lt;a href="https://sugarclub.sugarcrm.com/members/accushield"&gt;Joseph Moore&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;I recreated this on a demo system this morning and entered 26/01/2014 08:25am as my first date and 24/02/2014 09:25am as my second date, with my formula being &amp;quot;subtract(hoursUntil($date2),hoursUntil($date1))&amp;quot;. And this returned me 697.00 hours as expected.&lt;/p&gt;&lt;p&gt;The only thing I can think of as to why your getting 0 is if I were to do my formula the other way round, so doing &amp;quot;subtract(hoursUntil($date1),hoursUntil($date2))&amp;quot; instead. This is as it would give you a negative number, which depending on how you setup your field may result in it showing as a 0 (for example if you set the minimum value allowed to be 0). In this case you could simply put the &amp;quot;abs&amp;quot; function around the entire formula, which would cause it to always be a positive number.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13786?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 22:31:03 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:a899bc73-2273-4aaa-8614-57fd50d9a7e9</guid><dc:creator>Joseph Moore</dc:creator><description>&lt;p&gt;Liam,&lt;/p&gt;&lt;p&gt;Thanks for the reply, but since the dates in question are always in the past, hourUntil returns 0.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13785?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 22:22:02 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:cc0fb47f-387f-410a-b27f-afa557a707bc</guid><dc:creator>Liam Hastings</dc:creator><description>&lt;p&gt;Hi &lt;a href="https://sugarclub.sugarcrm.com/members/accushield"&gt;Joseph Moore,&lt;/a&gt;&lt;/p&gt;&lt;p&gt;You haven&amp;#39;t specified what version of Sugar you are on but in version 7.6.x there is a function for hoursUntil. This works the same as daysUntil where you simply wrap it around the date you check.&lt;/p&gt;&lt;p&gt;So to get a simple number of hours between you can &amp;quot;subtract(hoursUntil($date1),hoursUntil($date2))&amp;quot;. This will give you your required number of hours difference.&lt;/p&gt;&lt;p&gt;If you require this to be split into hours and minutes then there are two viable options that I can see.&lt;/p&gt;&lt;p&gt;1) Create a second calculated field that formats the one we just discussed for hours only. This allows you to have a human readable field on the views and a pure hours field to be able to report on (I would suggest not having the format field reportable).&lt;/p&gt;&lt;p&gt;2) Have to field customised by code development either by yourself or by getting a quote from your Sugar partner&amp;nbsp; (if they cannot perform this then contact SugarCRM). This would allow you to have option 1 as just 1 field, meaning it stores the hours in the database and just formats the data when viewing the records. To my knowledge this should then give you the readability and reporting as the reports use database queries.&lt;/p&gt;&lt;p&gt;I hope this helps, and sorry for not screens hots but I done this on my phone.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13784?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 19:27:55 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:acf79cdf-5cb2-4d6c-8e38-ac4b24988d1e</guid><dc:creator>Joseph Moore</dc:creator><description>&lt;p&gt;Actually, when I attempt to save that formula, I receive this error: &lt;span style="color:#444444;font-family:Arial, Verdana, Helvetica, sans-serif;font-size:12px;"&gt;concat: All parameters must be of type &amp;#39;string&amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13783?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 19:19:56 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:d5edcf02-6acf-47d3-874a-9986ff333c91</guid><dc:creator>Joseph Moore</dc:creator><description>&lt;p&gt;Thanks, Lori. That calculation is a beast, but I&amp;#39;ll see what I can do. :-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13781?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 19:17:33 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:900be054-c000-44a3-ac40-07ee3cc64880</guid><dc:creator>Joseph Moore</dc:creator><description>&lt;p&gt;Thanks, Ramon.&lt;/p&gt;&lt;p&gt;From this am I to understand that is is not possible when creating a calculated field from Studio, but requires PHP modification? (I am not a PHP programmer.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13782?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 19:15:54 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:af5a6448-4039-47f0-95f5-fe03bc328947</guid><dc:creator>Lori F</dc:creator><description>&lt;p&gt;Joseph,&lt;/p&gt;&lt;p&gt;We have an existing &lt;a href="https://web.sugarcrm.com/support/issues/69240" rel="nofollow" target="_blank"&gt;feature request&lt;/a&gt; asking to make calculations between datetime fields more readable.&amp;nbsp; The description in the feature request contains an example formula.&amp;nbsp; I haven&amp;#39;t fully tested it with your scenario but it may point you in the right direction.&lt;/p&gt;&lt;p&gt;Hope this helps!&lt;/p&gt;&lt;p&gt;Lori&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to calculate duration between two date/time fields?</title><link>https://sugarclub.sugarcrm.com/thread/13780?ContentTypeID=1</link><pubDate>Tue, 23 Feb 2016 19:10:52 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:1829107e-dce3-4c97-a0ba-9655b39b40b5</guid><dc:creator>Ramon Marcondes</dc:creator><description>&lt;p&gt;Hello, Joseph.&lt;/p&gt;&lt;p&gt;I would suggest you to do a diff between the two datetimes, and then use the &amp;quot;invert&amp;quot; method to check if there is positive difference between them.&lt;/p&gt;&lt;p&gt;You can get more information on these links:&lt;/p&gt;&lt;p&gt;&lt;a href="http://php.net/manual/en/datetime.diff.php" rel="nofollow" target="_blank"&gt;PHP: DateTime::diff - Manual&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://php.net/manual/en/class.dateinterval.php" rel="nofollow" style="text-decoration:underline;" target="_blank"&gt;http://php.net/manual/en/class.dateinterval.php&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Here is an example:&lt;/p&gt;&lt;pre class="php"&gt;$interval = $datetime1-&amp;gt;diff($datetime2);
if ($interval-&amp;gt;invert) { // returns true if difference is positive
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($interval-&amp;gt;h &amp;gt; 0) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return $interval-&amp;gt;h; // h is for &amp;#39;hour&amp;#39;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
}
&lt;/pre&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p&gt;Best regards,&lt;/p&gt;&lt;p&gt;Ramon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>