Jump to content

need time function


nezbo

Recommended Posts

Hi

 

is there any one out there that know the best way to add 2 times together including over midnight.

 

i am using a function that adds 2 time that are during the day.

 

i am not to sure how to get it to work if i have say 18:00 to 00:30.

 

the function that i have got works with whole hours and not part hours i'e' 00:15, 00:30, 00:45.

 

please help

 

neil

<?php
//function for geting the hours of a shift.
function hoursToBeWorked($startTimeTween, $endTimeTween)
{
if ($startTimeTween >= $endTimeTween)
{
	time ($hoursWorked = ($endTimeTween + '120000') - $startTimeTween);
}
else
{
	$hoursWorked =  $endTimeTween - $startTimeTween;		
}

return $hoursWorked;
}


?>

Link to comment
Share on other sites

Cheers Vijay

 

I think this would help, but i am not to sure how to use the time() stamp...

 

I have modified the function to look like this

<?php
//function for geting the hours of a shift.
function hoursToBeWorked($startTimeTween, $endTimeTween)
{
if ($startTimeTween >= $endTimeTween)
{
	$hoursWorked = ($endTimeTween + '120000') - $startTimeTween;
	$hoursWorked = $hoursWorked +10000;
}
else
{
	$hoursWorked =  $endTimeTween - $startTimeTween;	
	$hoursWorked = $hoursWorked +10000;	
}

return date("H:i:s", $hoursWorked);
}


?>

 

but it comes back with some strange figures :(

 

 

 

Hi..

        one suggestion store time in timestamp via time() function in php and then directly do the subtraction from big to small and then use it with date() function in whichever format you want..

 

        I hope this will help you..

 

Regards,

Vijay

Link to comment
Share on other sites

Hi again...

 

i have had another go and i get this

 

<?php
//function for geting the hours of a shift.
function hoursToBeWorked($startTimeTween, $endTimeTween)
{
if ($startTimeTween >= $endTimeTween)
{
	$hoursWorked = ($endTimeTween + '12000') - $startTimeTween;
}
else
{
	$hoursWorked =  $endTimeTween - $startTimeTween;	
}

return date("H:i:s", $hoursWorked);
}

$showTime = hoursToBeWorked("10:00:00", "11:00:00");

echo $showTime;
?>

 

the Result i get back is 01:00:01 and i am not to sure where the 1 second comes from.

 

Neil

Link to comment
Share on other sites

Cool Cheers

 

i am relitively new to PHP and SQL and i am not to sure what is ment by this, do you have any ideas of how to do it using mySQL, i have a start time and end time... so how would i use the date manipulation on SQL?

 

Neil

 

if you have enough queries left on your page I would advise that you use mysql to do this - its date manipulation support is VASTLY superior to that of php's...

Link to comment
Share on other sites

I am really sorry about this but i am starting to tear my hear out (well i have just had it cut) about this.

 

Please could some one post some script that i can edit to fit my code and a can understand... :'(

 

I have 2 times from a table in a mySQL database and all i need it to find the difrence the time format is 24hr and 00:00:00 h:i:s there has been some talk about TIMEDIFF() and i am not to sure how to work this, and i have looked at the link that ToonMariner cindly posted and i still cant work it out...

 

Please help a man in need :)

 

Cheers Neil

Link to comment
Share on other sites

;D ;D ;D ;D ;D

 

it works....

 

<?php
//function for geting the hours of a shift.
function hoursToBeWorked($startTimeTween, $endTimeTween)
{
if ($startTimeTween >= $endTimeTween)
{
	$hoursWorked =  strtotime($endTimeTween) - strtotime($startTimeTween);	
	return ($hoursWorked / 60 / 60) + 24;	
}
else
{
	$hoursWorked =  strtotime($endTimeTween) - strtotime($startTimeTween);
	return ($hoursWorked / 60 / 60);
}

}
?>;

Link to comment
Share on other sites

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.