0

Hi Guys

I have a select form element which splits an hour up into 4 segments like so

      '0.15' => t('0.15'),
      '0.30' => t('0.30'),
      '0.45' => t('0.45'),
      '1.0' => t('1.0'),

but when it adds it to the database table i get funny input like 14.6 after adding the 0.15 which should have given me 15.0 as the orginal value was 14.45 can someone tell me where i have gone wrong please

Thanks

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0

It's not recognizing it as "time" so what's happening is it's taking the 14.45 (which YOU understand to be 14h45m) and adds 0.15 (which YOU understand to be 15m) to give a 14.6. This makes sense if it's a base 60 number system...

Anyways, this will be helpful to you: http://stackoverflow.com/questions/1370332/add-two-or-more-time-strings-in-php (look at the second answer)

EDIT: I don't know what you're using this for, but I suggest converting the time into seconds or minutes or something like that and interpreting the number when you need to use it.. so store as x seconds but when it comes to displaying it in hours and minutes and seconds, use modular functions to find the number of total hours and minutes etc.

Edited by JameB

0

Heya djextreme5

thanks will look at the thread
what i want/need it for is a simple time log ie if someone spends 15 mins on a project then he/she can log that so in the end all time is logged on a per user basis 15mins is the shortest amount of time that they can spend hope that makes things clearer on what i'm trying to do here. sounds like maybe your edit would be the way to go is there an example i can look at ? as i'm pretty new to this

cheers

0

Google "adding time in PHP" and that should give you a lot of results. I suggest working in minutes for this.

So everytime someone chooses 30 minutes, you add 30 to existing number, 45 min => 45 to the existing number etc.

When you want to see the total number of minutes worked that are less than 60, it would be $extra_minutes = $total_minutes_worked % 60.

now AFTER you do the above, you look at what's remaning: $hours_worked = ($total_minutes_worked - $extra_minutes)/60;

This gives you both hours and minutes that someone worked... That's just how I would do it. I'm sure there's a better way! Google it! :)

Edited by JameB

1

This following bit would just be a cosmetic thing. I suggest you store the total period worked in just minutes as opposed to hours.minutes. But anyway:

$originalTime = '0.35'; //hrs.mins - from storage??
$addTime = 0.15; //from select (alternatively use 15 and get rid of 100 in totalminutes calculation)

function getNewTotal($hours,$addminutes){
    $minutes = 0;
    $hrs = 0;
    if (strpos($hours, '.') !== false){ 
        list($hours, $minutes) = explode('.', $hours); 
    } 
    $totalminutes = $hours * 60 + $minutes + $addminutes*100;
    $hrs = floor($totalminutes/60);     
    $mins = str_pad($totalminutes%60,2,'0',STR_PAD_LEFT);
    return $hrs . '.' . $mins;
} 

echo getNewTotal($originalTime,$addTime);

As mentioned, there's probably an easier way.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.