Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts

    adding time hh:mm

    I have the following function which adds hh:mm:ss.
    I am trying to modify it to just add hh:mm. But it never worked.
    can any one suggest any modifications so that it will do hh:mm addition


    PHP Code:
    function AddPlayTime ($oldPlayTime$PlayTimeToAdd) {

        
    $pieces split(':'$oldPlayTime);
        
    $hours=$pieces[0];
        
    $hours=str_replace("00","12",$hours);
        
    $minutes=$pieces[1];
       
    $seconds=$pieces[2];
        
    $oldPlayTime=$hours.":".$minutes.":".$seconds;

        
    $pieces split(':'$PlayTimeToAdd);
        
    $hours=$pieces[0];
        
    $hours=str_replace("00","12",$hours);
        
    $minutes=$pieces[1];
       
    $seconds=$pieces[2];

        
    $str $str.$minutes." minute".$seconds." second" ;
        
    $str "01/01/2000 ".$oldPlayTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ;

        
    // Avant PHP 5.1.0, vous devez comparer avec  -1, au lieu de false
        
    if (($timestamp strtotime($str)) === false) {
            return 
    false;
        } else {
            
    $sum=date('h:i:s'$timestamp);
            
    $pieces split(':'$sum);
            
    $hours=$pieces[0];
            
    $hours=str_replace("12","00",$hours);
            
    $minutes=$pieces[1];
            
    $seconds=$pieces[2];
            
    $sum=$hours.":".$minutes.":".$seconds;

            return 
    $sum;

        }


  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Here's my shot at it:

    PHP Code:
    <?php

    // numbers for testing
    $o="12:59";
    $p="0:58";

    // display for testing
    echo "$o<br />";
    echo 
    "$p<br />";
    echo 
    AddPlayTime($o,$p);

    // FUNCTION - ADD HOURS and MINUTES
    function AddPlayTime ($oldPlayTime$PlayTimeToAdd) {
    $old=explode(":",$oldPlayTime);
    $play=explode(":",$PlayTimeToAdd);

    $hours=$old[0]+$play[0];
    $minutes=$old[1]+$play[1];
    if(
    $minutes 59){
    $minutes=$minutes-60;
    $hours++;
    }
    if(
    $minutes 10){
    $minutes "0".$minutes;
    }
    if(
    $minutes == 0){
    $minutes "00";
    }
    $sum=$hours.":".$minutes;
    return 
    $sum;
    }

     
    ?>

  • Users who have thanked mlseim for this post:

    gadaschowdary (01-07-2010)

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks a lot...

  • #4
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    hey mlseim,

    the drop down list starts from 08:20...
    can it start displaying from 08:00 and then add on..

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Quote Originally Posted by gadaschowdary View Post
    hey mlseim,

    the drop down list starts from 08:20...
    can it start displaying from 08:00 and then add on..
    I don't know what that means.
    What is your drop down list?

    What is this script for, and do you have a web page we can see?

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    function AddPlayTime($old$add) {
        list(
    $oH$oM) = explode(':'$old);
        list(
    $aH$aM) = explode(':'$add);
        
    $hours = ($oH $aH floor(($oM $aM) / 60)) % 24;
        
    $mins = ($oM $aM) % 60;
        return 
    str_pad($hours2'0'STR_PAD_LEFT).':'.str_pad($mins2'0'STR_PAD_LEFT);

    Is that what you want? ie go back to 00 at 24 hrs?
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    He's talking about adding time.
    Adding hours and minutes together.

    There was no mention of grabbing the current time and adding hours and minutes.
    He specifically mentions adding a "playtime" to an old "playtime" and display the sum.

    So, after playing for many days, that sum could be 325:45 (325 hours, 45 minutes).

    That's the way I'm interpreting it.

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Oh. In that case,
    PHP Code:
    $hours = ($oH $aH floor(($oM $aM) / 60)) % 24
    should be
    PHP Code:
    $hours = ($oH $aH floor(($oM $aM) / 60)); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    I don't know what that means.
    What is your drop down list?

    What is this script for, and do you have a web page we can see?
    hey guyz,


    first of all im not he.
    coming to the display of times, i am not grabing current time. It is retrieved from database.
    this is a golf tee time reservation system.
    based on the duration set, reservation time should display slots in the DDL starting from the start time and then the duration added to it iteratively.



    [php]
    navDbQuery("golf.config", "", "configrecid");

    //$query = sprintf("SELECT * FROM golf.config WHERE configrecid = '1'");
    // $result = db_query($query);

    if(navHaveRows()) {
    $startTime = $_SESSION['row']['starttime'];
    echo "starttime from database is $startTime";
    $durationTime = $_SESSION['row']['duration'];
    $sum = AddPlayTime($startTime,$durationTime);
    <td align="right"> Reservation Time :&nbsp;</td>
    <td align="left"><select name="reservtime" type="text" class="textbox" id="reservtime" value="" size="1">
    <?
    for($i = 1; $i < 12; $i++)
    {?>
    <option value="<?echo $sum?> "><?echo $sum?> </option>
    <?
    $sum = AddPlayTime($sum,$durationTime);
    [php]


    i dont know how to add a image. else i can add my web page so that it would be clear.
    Last edited by gadaschowdary; 01-07-2010 at 08:22 PM.

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Well, in that case, I guess we need to see the script that generates the drop-down.

    I assume that script queries the database and then generates the form?

    It also changes my script ... I don't display the time as in time of day.
    Also, are your times in 24hr mode or AM/PM mode?

    This discovery sort of changes everything.

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by gadaschowdary View Post
    first of all im she.

    coming to the display of 08:00
    what i am trying to do is get the start time and duration from database.
    then display times for reservation by adding the starttime and duration.
    duration can be 10 20 or 30 mins.
    so the ddl looks like this
    PHP Code:
    list($h$m$s) = explode(":",$PlayTimeToAdd);
    $result strtotime("+ $h hours $m minutes $s seconds",$OldPlayTime); 
    all you want to do is to be sure that $OldPlayTime is a unix timestamp, convert it if is not, and to be sure that $PlayTimeToAdd is hh:mm or hh:mm:ss formated.

    best regards

  • #12
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Well, in that case, I guess we need to see the script that generates the drop-down.

    I assume that script queries the database and then generates the form?

    It also changes my script ... I don't display the time as in time of day.
    Also, are your times in 24hr mode or AM/PM mode?

    This discovery sort of changes everything.
    noo your code has fit in my script.

    it is generating the times as i want.

    only thing is to display the start time also.

    This is how i am generating the drop down list by using your code.

    [php]
    function AddPlayTime ($oldPlayTime, $PlayTimeToAdd) {
    $old=explode(":",$oldPlayTime);
    $play=explode(":",$PlayTimeToAdd);

    $hours=$old[0]+$play[0];
    $minutes=$old[1]+$play[1];
    if($minutes > 59){
    $minutes=$minutes-60;
    $hours++;
    }
    if($minutes < 10){
    $minutes = "0".$minutes;
    }
    if($minutes == 0){
    $minutes = "00";
    }
    $sum=$hours.":".$minutes;
    return $sum;
    }

    navDbQuery("golf.config", "", "configrecid");


    if(navHaveRows())
    {
    $startTime = $_SESSION['row']['starttime'];
    //echo "starttime from database is $startTime";
    $durationTime = $_SESSION['row']['duration'];
    $sum = $startTime;
    $sum = AddPlayTime($startTime,$durationTime);
    }
    if ($sum!=false)
    {
    //echo $startTime." + ".$durationTime." === ".$sum;


    ?>[php]

    <td align="right"> Reservation Time :&nbsp;</td>
    <td align="left"><select name="reservtime" type="text" class="textbox" id="reservtime" value="" size="1">
    [php] <?
    for($i = 1; $i < 12; $i++)
    {?>
    <option value="<?echo $sum?> "><?echo $sum?> </option>
    <?
    $sum = AddPlayTime($sum,$durationTime);//$sum. " + ".$durationTime;
    }?>[php]
    </select></td>
    </tr>
    Last edited by gadaschowdary; 01-07-2010 at 08:48 PM.

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You're adding the duration to the start time right away ...
    comment-out that line.

    PHP Code:
    <?php

    function AddPlayTime ($oldPlayTime$PlayTimeToAdd) {
    $old=explode(":",$oldPlayTime);
    $play=explode(":",$PlayTimeToAdd);

    $hours=$old[0]+$play[0];
    $minutes=$old[1]+$play[1];
    if(
    $minutes 59){
    $minutes=$minutes-60;
    $hours++;
    }
    if(
    $minutes 10){
    $minutes "0".$minutes;
    }
    if(
    $minutes == 0){
    $minutes "00";
    }
    $sum=$hours.":".$minutes;
    return 
    $sum;
    }

    navDbQuery("golf.config""""configrecid");


    if(
    navHaveRows())
    {
    $startTime $_SESSION['row']['starttime'];
    //echo "starttime from database is $startTime";
    $durationTime $_SESSION['row']['duration'];
    $sum $startTime;

    // Don't add this here ... you want the start time to appear first.
    // $sum = AddPlayTime($startTime,$durationTime);
    }
    if (
    $sum!=false)
    {
    //echo $startTime." + ".$durationTime." === ".$sum;
    ?>

    <td align="right"> Reservation Time :&nbsp;</td>
    <td align="left"><select name="reservtime" type="text" class="textbox" id="reservtime" value="" size="1">

    <?
    for($i 1$i 12$i++)
    {
    ?>
    <option value="<?echo $sum?> "><?echo $sum?> </option>
    <?
    $sum 
    AddPlayTime($sum,$durationTime);//$sum. " + ".$durationTime;
    }?>
    </select></td>
    </tr>

  • Users who have thanked mlseim for this post:

    gadaschowdary (01-07-2010)

  • #14
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Woww!! It worked

    thanx

  • #15
    New Coder
    Join Date
    Oct 2009
    Location
    Dallas,Tx USA
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    hey msleim,

    i have a question
    if the mintues are less than 10 we are adding 0 to it right! but still it is not getting displayed..
    it is displaying like this 8:10, 8:20..
    I want it as 08:00, 08:20..
    .can you take look at it plz...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •