Working Timesheet

Est. Reading: 3 minutes
By: FDM Digital
Created: 07/16/2018
Category: ,
Difficulty: Intermediate

Working Timesheet

×Warning: This tutorial was created 2338 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

Create A Staff Timesheet Using Formidable Forms

This article details how to create a basic staff timesheet using Formidable Forms, ideal for home or remote workers or anyone needing to record dates and times in an online form. This article assumes that you are using the latest version of Formidable Pro and know how to add PHP snippets to your site. Along with a basic understanding of HTML and CSS.

This timesheet application consists of three key parts:

  1. A form for entering the dates and times worked
  2. A filter form for filtering the dates viewable (optional)
  3. A view for displaying the information

We built this application using the following steps:

Step 1: Build the initial form

Create your form for users to log their times and dates worked. Include a hidden / admin only field for the time calculation to be inserted. Then add the following PHP snippet to your functions.php file, a custom plugin or the code snippets plugin:

/* Calculate Time Worked */
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
  if($field->id == 25){ //change 25 to the ID of the hidden or admin only field which will hold the calculation
    	$start = (strtotime($_POST['item_meta'][23])); //change 23 to the ID of the first field
    	$end = (strtotime($_POST['item_meta'][24])); //change 24 to the ID of the second field
$lunch = (strtotime($_POST['item_meta'][25])); //change 25 to the ID of the lunch hour field
    $totaltime = ($end – ($start + $lunch));
    $hours = intval($totaltime / 3600);   
    $seconds_remain = ($totaltime - ($hours * 3600)); 
    $minutes = intval($seconds_remain / 60);
    //$seconds = ($seconds_remain - ($minutes * 60)); Uncomment this line if you want seconds calculated.
    $leading_zero_for_minutes = $minutes < 10 ? '0' : '';
    //$leading_zero_for_seconds = $seconds < 10 ? '0' : '';//Uncomment this line if you're including seconds.
    $totaltime = $hours . ':' . $leading_zero_for_minutes . $minutes;	
    //$totaltime = $hours . ':' . $leading_zero_for_minutes . $minutes . ':' . $leading_zero_for_seconds . $seconds;//uncomment this line if you're including seconds
    $value = $_POST['item_meta'][26] = $totaltime; //change 26 to the ID of the hidden or admin only field which will hold the calculation
  }
  return $errors;
}

This code will calculate the number of hours worked for each period and insert the value into a hidden or admin only field within your form. This is a slightly edited version of the original snippet which can be found on the Formidable KB HERE.

Step 2: Build a Search form

This stage is optional if you want to be able to search and filter your results in your view (which we did).Create a new search form with any fields you wish to search on. Our search form is very basic and consists of 2 date fields (start_date and end_date). In the form settings we set the following options:

  1. Select “Do not store entries submitted from this form”
  2. Change the ‘On Submit’ option to ‘Redirect to URL’
  3. In the URL enter www.YourSite.com/YourView/?start_date=[551]&end_date=[552] and change 552 and 553 to the ID’s of your 2 date fields.
Step 3: Building Your View

Now you can create your view which will display the hours worked. You may wish to create 2 Views depending on your requirements as it may be useful to have 1 view for the employee to view their own hours and a 2nd view for Administrators to view everyone’s working hours.

In our view, under basic settings, we have selected Form 1 (our time sheet form) and ‘All Entries’ as the view type. Our view has been set up as a html table, but you may wish to build yours differently.

In order to calculate the total time worked for the numbers of rows on display in the table, we have created a new code snippet that will create a total of the hidden fields from Form 1 and add display them in a shortcode – [sum_544] – in the ‘After Content’ section of the view.

VIEW HERE

Change 7808 to the ID of your view and 544 to the ID of your hidden field in 2 places. This snippet will allow you to only total what is being displayed in the view and therefore will always display the correct total based on the filters being used.

That's pretty much it for the basics. For a more in-depth look at how this was achieved you can view the full article on our website HERE.

Thanks

Chris

Leave a Reply

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crosschevron-leftchevron-rightarrow-right