Page 1 of 1

working with time and calculations

Posted: Fri Aug 21, 2009 9:11 am
by rob1
having never worked with time before on any projects i thought it wise to get some pointers before i make a start so i can avoid any common mistakes and pitfalls.

the project im working on is going to be a simple recording / billing system, where by i have a set number of task codes that i can bill against - all very simple stuff.

However, i have a "standard unit field" within my db billing table so certain tasks will be charged at different intervals.

ie "call out" rates might be charged in blocks of 15 minutes or 30 minutes, so if i enter in 18 minutes then it would automatically round up to the next "unit", which the next value would be 30 minutes (if that makes any sense?).

What would be easiest/best practise way of rounding up/storing these time increments, but also prevent historical data from change, so should i choose to increase/decrease my hourly rate ?

Re: working with time and calculations

Posted: Fri Aug 21, 2009 9:27 am
by lord_webby

Code: Select all

<?php
$minutes = 18; //minutes you worked
 
switch ($minutes) {
    case (0 < $minutes < 15):
        $i ="0.25"; //quarter of an hour
        break;
    case (15 < $minutes < 30):
        $i = "0.5";
        break;
    case (30 < $minutes < 45):
        $i ="0.75";
        break;
    case (45 < $minutes < 60):
        $i ="1";
        break; 
}
 
$rate = "75"; //pounds per hour
$description = "Website work for Joe Bloggs";
$invoice_amount = $rate * $i;
$date = date("Y-m-d"); // "2009-08-21" <- mysql date format
?>
Then use a DB such as MySQL:

Code: Select all

 
INSERT INTO invoices (id, description, hours, rate, amount, date) 
VALUES (NULL, '$description', '$i', '$rate', '$invoice_amount', '$date');
That should get you started. But to be honest I'd use an excel spreadsheet. Accountants like to have spreadsheets. Although you could write an export to XLS (EXCEL) function for your data.

Re: working with time and calculations

Posted: Fri Aug 21, 2009 10:16 am
by rob1
this is just part of a bigger project and the intention is to create a reporting section which will display the results on screen aswell as allow you to export it into another format - such as xls.

is there a easier or better way to make the code more clever so say i had the standard unit for a task code as 15 minute intervals, but should i want to increase these intervals to 30 minutes I would need to redo the code.

i had initially thought of someway to read the timestamps, maybe with end timestamp-starttime stamp in some way, but you have given me some ideas to help stimulate my thoughts.

Re: working with time and calculations

Posted: Fri Aug 21, 2009 10:38 am
by lord_webby

Code: Select all

<?php
$minutes = 18; //minutes you worked
$interval = 15; //15 minutes
$d = "0.25" // interval in decimal form
switch ($minutes) {
    case (0 < $minutes < $interval):
        $i = $d; //quarter of an hour
        break;
    case ($interval < $minutes < ($interval * 2)):
        $i = ($d*2);
        break;
    case (($interval * 2) < $minutes < ($interval * 3)):
        $i = ($d*3);
        break;
    case (($interval * 3) < $minutes < ($interval * 4)):
        $i = ($d*4);
        break;
}
 
$rate = "75"; //pounds per hour
$description = "Website work for Joe Bloggs";
$invoice_amount = $rate * $i;
$date = date("Y-m-d"); // "2009-08-21" <- mysql date format
?>

Re: working with time and calculations

Posted: Fri Aug 21, 2009 10:44 am
by lord_webby
If you want to expand on that you'll have to do it yourself, however. Personally I'd put it into a function and rearrange it to use a greater timescale. But the logic is there.