...

View Full Version : adding time hh:mm



gadaschowdary
01-06-2010, 03:42 PM
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




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;

}
}

mlseim
01-06-2010, 05:05 PM
Here's my shot at it:



<?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;
}

?>

gadaschowdary
01-07-2010, 06:25 PM
thanks a lot...

gadaschowdary
01-07-2010, 06:32 PM
hey mlseim,

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

mlseim
01-07-2010, 06:56 PM
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?

JAY6390
01-07-2010, 07:55 PM
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($hours, 2, '0', STR_PAD_LEFT).':'.str_pad($mins, 2, '0', STR_PAD_LEFT);
}Is that what you want? ie go back to 00 at 24 hrs?

mlseim
01-07-2010, 07:59 PM
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.

JAY6390
01-07-2010, 08:01 PM
Oh. In that case,

$hours = ($oH + $aH + floor(($oM + $aM) / 60)) % 24;
should be

$hours = ($oH + $aH + floor(($oM + $aM) / 60));

gadaschowdary
01-07-2010, 08:10 PM
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.

mlseim
01-07-2010, 08:18 PM
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.

oesxyl
01-07-2010, 08:23 PM
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
http://sindhuragada/desktop/ddl.jpg



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

gadaschowdary
01-07-2010, 08:40 PM
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>

mlseim
01-07-2010, 08:55 PM
You're adding the duration to the start time right away ...
comment-out that line.



<?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>

gadaschowdary
01-07-2010, 09:01 PM
Woww!! It worked

thanx

gadaschowdary
01-08-2010, 10:36 PM
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...

mlseim
01-08-2010, 11:19 PM
You mean the hours?
Try the new script below.




<?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";
}
if($hours < 10){
$hours = "0".$hours;
}
if($hours == 0){
$hours = "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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum