Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 12:45 PM   PM User | #1
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
datetime difference caluculation for caluculatin SLA in time

HI All,

As subject line says,

from the below I could successfully calculate days in to hours, time but I am able to calculate getting in to Minutes using below code.

Code:
//to claculate SLA
$datetime1=$date1; //first date
$datetime2=$date2; //second date
		
		define("SECONDS_PER_HOUR", 24*60*60);
        // Calculate timestamp
		$start = strtotime($datetime1);
		$stop = strtotime($datetime2);
        // Diferences
		$difference = $stop - $start;
		// Hours
		$hours = round($difference / SECONDS_PER_HOUR, 0, PHP_ROUND_HALF_DOWN);
		$hourmul=($hours*24);

		// Minutes
		$minutes = ($difference/ SECONDS_PER_HOUR);
		$minmul= round(($minutes/60));
		// Seconds
		$seconds = ($difference % SECONDS_PER_HOUR) / 60;
		$secmul= round(($seconds/60));
		$var= $hourmul. ":" .$minmul. ":" .$secmul;
please help to complete this function my desire output should be, for example.

Start date = 05-11-2012 23:15:00
end date= 06-11-2012 01:15:00

output should be 2:00:00 of time.

can any one help me ???

Regards,
nani

Last edited by nani_nisha06; 11-05-2012 at 01:54 PM..
nani_nisha06 is offline   Reply With Quote
Old 11-05-2012, 01:36 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Why not just use a DateInterval:
PHP Code:
date_default_timezone_set('UTC');

$dts = new DateTime('05-11-2012 23:15:00');
$dte = new DateTime('06-11-2012 01:15:00');

$di $dte->diff($dtstrue);
print 
$di->format('%H:%I:%S'); 
The object properties can be inspected as well to see what to show. If its <> 0, then its different in some way shape or form, so you can tack on things like years and months systematically this way.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nani_nisha06 (11-05-2012)
Old 11-05-2012, 01:53 PM   PM User | #3
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Why not just use a DateInterval:
PHP Code:
date_default_timezone_set('UTC');

$dts = new DateTime('05-11-2012 23:15:00');
$dte = new DateTime('06-11-2012 01:15:00');

$di $dte->diff($dtstrue);
print 
$di->format('%H:%I:%S'); 
The object properties can be inspected as well to see what to show. If its <> 0, then its different in some way shape or form, so you can tack on things like years and months systematically this way.
Thanks Fou-Lu,

This was a very simple logic but I was writing all different though of mine which took this all time.....need be more smart & sharp thinker from the next case....

even I have tried above code my self twice but was doing silly mistake make me open this ticket...

Thanks for you help...

Regards,
Nani
nani_nisha06 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 PM.


Advertisement
Log in to turn off these ads.