Calculating task duration using the calendar

Unofficial documentation how to do interesting things and work around problems in ProcessMaker

Moderator: amosbatto

Forum rules
Unofficial documentation for features which have not been tested by Quality Assurance or may change in future versions of ProcessMaker
Posts: 5992
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Calculating task duration using the calendar

Unread postby amosbatto » Tue Mar 13, 2018 6:10 pm

If needing a function to calculate the amount of time that a task takes, based on the calendar of the task or the assigned user, edit the source code found in workflow/engine/classes/class.pmFunctions.php with a plain text file. At the bottom of the file add the following code:

Code: Select all

Function to break time in seconds into days, minutes, hours and seconds.
   int $seconds: The number of seconds.
   string $timeUnits: The unit of time which can be "seconds", "minutes", "hours", "days",
                      "array" or "string".
Return Value:
   Returns the time according to the $timeUnits. If "string", then returns something like
   "1 day 7 hours 0 minutes 34 seconds" or "45 minutes 1 second". Units which are set to 0 
   not included in the string.
   If "array", then returns something like:
     "days" => 1,
     "hours" => 7,
     "minutes" => 0,
     "seconds" => 34
function timeBreakdown($seconds$timeUnits='seconds') {
$timeUnits strtolower($timeUnits);
   switch (
$timeUnits) {
$aTime = array();
$aTime['days'] = (int) ($seconds 86400);
$remainder $seconds 86400;
$aTime['hours'] = (int) ($remainder 3600);
$remainder $remainder 3600;
$aTime['minutes'] = (int) ($remainder 60);
$aTime['seconds'] = $remainder 60;
         if (
$timeUnits == 'array')
         else {
$sTime '';
            if (
$aTime['days'] > 1)
$sTime $aTime['days'].' days';
            elseif (
$aTime['days'] == 1)
$sTime '1 day';             
            if (
$aTime['hours'] > 1)
$sTime .= (empty($sTime) ? '' ' ') . $aTime['hours'].' hours';
            elseif (
$aTime['hours'] == 1)
$sTime .= (empty($sTime) ? '' ' ') . '1 hour';
            elseif (!empty(
$sTime .= ' 0 hours';  
            if (
$aTime['minutes'] > 1)
$sTime .= (empty($sTime) ? '' ' ') . $aTime['minutes'].' minutes';
            elseif (
$aTime['minutes'] == 1)
$sTime .= (empty($sTime) ? '' ' ') . '1 minute';
            elseif (!empty(
$sTime .= ' 0 minutes';  
            if (
$aTime['seconds'] > 1)
$sTime .= (empty($sTime) ? '' ' ') . $aTime['seconds'].' seconds';
            elseif (
$aTime['seconds'] == 1)
$sTime .= (empty($sTime) ? '' ' ') . '1 second';
            elseif (!empty(
$sTime .= ' 0 seconds';
         throw new 
Exception("Unrecognized time unit '$timeUnits'");

PMFTaskDuration() calculates the amount of time a task has taken, depending
on the calendar which is configured for the task or the assigned user.
  $userUid:    Unique ID of user assigned to the task. Default is the current logged-in user.
  $processUid: Unique ID of the process. Default is the current process.
  $taskUid:    Unique ID of the task whose duration will be calculated. 
  $startTime:  Time when task started in YYYY-MM-DD HH:MM:SS format.
  $endTime:    Time when the task ended in YYYY-MM-DD HH:MM:SS format. 
               If left blank or not included, then set to current time
  $timeUnits:  The unit of time which is returned by the function, which can be:
               "seconds" (default), "minutes", "hours", "days", "string" or "array"
function PMFTaskDuration($userUid$processUid
$taskUid$startTime$endTime$timeUnits 'seconds') {
   if (empty(
$endTime)) {
//change this line to work correctly for current time in the Enterprise Edition:
$endTime date("Y-m-d H:i:s"); 
$g = new G();       
$oCal = new Calendar();
$seconds $oCal->calculateDuration($startTime$endTime);

Here are two examples for how to call the PMFTaskDuration() function in a trigger:

Example1: Check how much time has elapsed in the current task:

Code: Select all

$c = new Cases();
$aCase $c->LoadCase(@@APPLICATION, @%INDEX);
TaskSeconds PMFTaskDuration(@@USER_LOGGED, @@PROCESS, @@TASK$aCase['DEL_DELEGATE_DATE']); 

Example2: Display a grid of tasks, sorted by task and user:

Code: Select all

$aTasks executeQuery($sql);

//Calculate task times:
for ($i=1$i <= count($aTasks); $i++) {
$aTasks[$i]['TASK_TIME'] = PMFTaskDuration(
TasksGrid $aTasks;
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest