Doing time calculations...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Reljac

    Doing time calculations...

    Howdy,

    For a project I am working on right now I need to be able to calculate the
    time difference between to given times.
    (For example...The difference between 11:30 am and 1:45pm being 2:15)

    Right now the input is setup as 2 text boxes, 2 dropdowns, and 2 radio
    groups. The text boxes get the hour entered into them, the drop downs holdd
    a list contaning 00, 15, 30, and 45 (the minutes) and the radio groups being
    am or pm.

    Is it possible to use that configuration and somehow get the time
    difference? or do I need to change the input? The goal is to be able to
    enter the two values and immediately see a calculated difference in a third
    textbox on the page.

    I'm thinking something like....
    User enters hour in first text box(textHFrom) - 11
    User selects minue value from second dropdown(cboMFr om) - 30
    User selects am/pm from radio group(radioF) - am
    User enters hour in second text box(textHTo) - 1
    User selects minue value from second dropdown(cboMTo ) - 45
    User selects am/pm from radio group(radioT) - pm

    onChange of second RadioGroup time is calculated and result in inserted into
    third text box(textHours) - 2:15

    I'm not sure if/how to bet do this.

    Thanks,
    Jason


  • Nikolai Chuvakhin

    #2
    Re: Doing time calculations...

    "Jason Reljac" <jmr109@hotmail .com> wrote in message
    news:<vhlp3hspb m19aa@corp.supe rnews.com>...[color=blue]
    >
    > For a project I am working on right now I need to be able to calculate the
    > time difference between to given times.
    > (For example...The difference between 11:30 am and 1:45pm being 2:15)[/color]

    Read up on strtotime() function:

    Parse about any English textual datetime description into a Unix timestamp


    You'll be surprised how much it lets you get away with. Using this
    function, you can actually compute the difference between 'last
    Friday 5:37 PM' and '+2 hours 14 minutes' (meaning, 2 hours 14 minutes
    from now), with both arguments specified as shown.

    So let's say you have a form that has two text inputs, 'start' and
    'end'. To compute the time difference, you would do something like
    this:

    $start = strtotime ($_GET['start']);
    if ($start == -1) {
    die ('Invalid start time');
    }
    $end = strtotime ($_GET['end']);
    if ($end == -1) {
    die ('Invalid end time');
    }
    $diff = $end - $start;

    Now $diff contains the difference between two times stated in seconds.
    Converting it into minutes and hours should be rather trivial...

    Cheers,
    NC

    Comment

    • Jason Reljac

      #3
      Re: Doing time calculations...

      Many, many thanks...with a small change, that worked just right.

      Jason
      "Nikolai Chuvakhin" <nc@iname.com > wrote in message
      news:32d7a63c.0 307201726.78802 80f@posting.goo gle.com...[color=blue]
      > "Jason Reljac" <jmr109@hotmail .com> wrote in message
      > news:<vhlp3hspb m19aa@corp.supe rnews.com>...[color=green]
      > >
      > > For a project I am working on right now I need to be able to calculate[/color][/color]
      the[color=blue][color=green]
      > > time difference between to given times.
      > > (For example...The difference between 11:30 am and 1:45pm being 2:15)[/color]
      >
      > Read up on strtotime() function:
      >
      > http://php.net/strtotime
      >
      > You'll be surprised how much it lets you get away with. Using this
      > function, you can actually compute the difference between 'last
      > Friday 5:37 PM' and '+2 hours 14 minutes' (meaning, 2 hours 14 minutes
      > from now), with both arguments specified as shown.
      >
      > So let's say you have a form that has two text inputs, 'start' and
      > 'end'. To compute the time difference, you would do something like
      > this:
      >
      > $start = strtotime ($_GET['start']);
      > if ($start == -1) {
      > die ('Invalid start time');
      > }
      > $end = strtotime ($_GET['end']);
      > if ($end == -1) {
      > die ('Invalid end time');
      > }
      > $diff = $end - $start;
      >
      > Now $diff contains the difference between two times stated in seconds.
      > Converting it into minutes and hours should be rather trivial...
      >
      > Cheers,
      > NC[/color]


      Comment

      Working...