Jump to content

Recommended Posts

Ok so I basically have two textfields and a submit button.

 

The first text field basically allows the user to input a duration of an event in the format of Hours:Minutes:Seconds such as: 02:30:00 is two and a half hours.

 

The second textfield allows the user to put in an average time 'something' does something in the same format as above. So for example 00:20:00 is 20 minutes.

 

1st Textfield:

<input name="tmarrtextfield" type="text" class="textbox" id="tmarrtextfield" value="00:00:00">

 

2nd Textfield:

<input name="tmatttextfield" type="text" class="textbox" id="tmatttextfield" value="00:00:00">

 

What I need is for when the form is submitted to the same page.. the code to...:

 

1. Convert the normal text info into actual time data.

2. Divide the duration by the average timing so for example: 02:30:00 (150 minutes) / 00:20:00 (20 minutes) = 7.5 (Would return a rounded down number or up, doesn't matter) then store the number as a variable that will be echoed later.

 

Harry.

Link to comment
https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/
Share on other sites

for 2nd

<?php
function my_sec($a){
    $a = explode(':', $a);
    $out = $a[0];
    for($i=1;$i<count($a);$i++){
        $out *= 60;
        $out += $a[$i];
    }
    return $out;
}
$time1 = '02:30:00';
$time2 = '30:00';
echo my_sec($time1) / my_sec($time2);
?>

Thank you Crayon, I did knock up my own script before reading the submition by sasa which worked. ;)

 

Sasa... That works brilliantly. What would I need to add to the script to finalise it slightly.

 

As currently you just have a count of the overall amount of movements, I would like to add another count but of movements possible per hour. How can I achieve this?

If I try 02:30:00 and 00:30:00 I get an answer of 2.5 - Not sure how I can iron that lil problem out there as well you should get 2 movements an hour.

 

Also when I put the:

 

$hourresult = my_sec($time1) / 3600;

 

Under the rest of the code, it messes up the original full count and only shows the per hour.

I would edit but I cant so I will have to re-post in accordance to what you said sasa.

 

Here is the code in full including the last bit for the movements per hour:

 

// TIME FUNCTION FOR THE ARRIVALS
function my_sec($a){
    $a = explode(':', $a);
    $out = $a[0];
    for($i=1;$i<count($a);$i++){
        $out *= 60;
        $out += $a[$i];
    }
    return $out;
}
$time1 = $_POST['tmarrtextfield'];
$time2 = $_POST['tmatttextfield'];
$numberresult = my_sec($time1) / my_sec($time2);
$hourresult = my_sec($time1) / 3600;

 

Problem: If for example I put in 30 minutes and click the button it will come up with an answer of 3 for moves per hour, which is impossible.

 

Or.

 

If I put in 20 minutes and try to get the moves per hour I get a return of 2, which is incorrect too...  :confused:

 

Harry.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.