Jump to content

Archived

This topic is now archived and is closed to further replies.

sun14php

date difference ...

Recommended Posts

look at below: my data stored in mysql table.

LogId Date         Time     SrvID    Problem Deatils       Action     Status(5 min) Status TimeTaken    Rmarks
54 30-06-2006  13:49:03 #sdla12    cpu         94 server staff informed


now i want to calculate  filed TimeTaken , that  should be calculated as TimeTaken=Time-Current system Time; preferbly in minutes usinh php. guide me please :P



Share this post


Link to post
Share on other sites
[code]//$time is hh-mm-ss (hour-min-sec) like 13:49:03
//$date is dd-mm-yyyy like 30-06-2006
$arr_time=explode(":",$time);
$arr_date=explode("-",$date);
if($arr_date[2]%4==0){$dst=1;} else{$dst=0;}; //for daylight savings
$time_in_db=mktime($arr_time[0],$arr_time[1],$arr_time[2],$arr_date[1],$arr_date[0],$arr_date[2],$dst);
$now=time();
$time_took=$now-$time_in_db; //this is what you need[/code]

Orio.

Share this post


Link to post
Share on other sites
If the two times are in the database what wrong with my code please cheers.

<?
$time="01:00:00";
$time_tacken="03:00:00";

$checked_out=$time_tacken-$time;

echo "<br> this is the time it took $checked_out<br>";

?>

Share this post


Link to post
Share on other sites
But it sometimes takes more than one day, redarrow.
My script works with a range of unlimited dates (From 1/1/1970 until unlimited). But it returns a timestamp. But then you can use the date() function to return a normal date.

Orio.

Share this post


Link to post
Share on other sites
orio can you kindly brake your code right down please and post it without the [code tags] cheers.

Share this post


Link to post
Share on other sites
//$time is hh-mm-ss (hour-min-sec) like 13:49:03
//$date is dd-mm-yyyy like 30-06-2006

$arr_time=explode(":",$time);
$arr_date=explode("-",$date);

if($arr_date[2]%4==0){$dst=1;} else{$dst=0;}; //for daylight savings

$time_in_db=mktime($arr_time[0],$arr_time[1],$arr_time[2],$arr_date[1],$arr_date[0],$arr_date[2],$dst);
$now=time();
$time_took=$now-$time_in_db; //tells you how many seconds it took

Share this post


Link to post
Share on other sites
i wont to learn it properly cheers.



//$time is hh-mm-ss (hour-min-sec) like 13:49:03
//$date is dd-mm-yyyy like 30-06-2006

$arr_time=explode(":",$time);// what this mean get the time explode?

$arr_date=explode("-",$date);// what this mean get the date exsplode?

if($arr_date[2]%4==0) { //exsplin this line please

$dst=1;//what this?

}else{

$dst=0;// what this do?

};


$time_in_db=mktime($arr_time[0],$arr_time[1],$arr_time[2],$arr_date[1],$arr_date[0],$arr_date[2],$dst);// whats mktime doing?

$now=time();//this?

$time_took=$now-$time_in_db;

Share this post


Link to post
Share on other sites
no orio i could not get ur script . please explain per my first query:

how do i display time in minutes only ?

Share this post


Link to post
Share on other sites
[code=php:0]//$time is hh-mm-ss (hour-min-sec) like 13:49:03
//$date is dd-mm-yyyy like 30-06-2006

$arr_time=explode(":",$time);// Lets say the time is 13:49:03 it'll create an array (keys from 0-2) that its values are 13,49,03. It splits with explode, sperator is ":". Read more about explode on ww.php.net

$arr_date=explode("-",$date);// Lets say the date is 30-06-2006 it'll create an array (keys from 0-2) that its values are 30,06,2006. It splits with explode, sperator is ":". Read more about explode on ww.php.net

if($arr_date[2]%4==0) { //Check if the modular of the year when you devide it by 4, to see if it's a leap year.

$dst=1; //daylight savings is true

}else{

$dst=0;//daylight savings is false

};


$time_in_db=mktime($arr_time[0],$arr_time[1],$arr_time[2],$arr_date[1],$arr_date[0],$arr_date[2],$dst); //mktime- creats a timestamp when you give it a date. Read more on www.php.net

$now=time(); //Get the current timestamp. Read more on www.php.net

$time_took_secs=$now-$time_in_db; //decrease from the current timestamp the timestamp we made with mktime.

//if you want to see how many mins:
time_took_mins=round($time_took_secs/60);[/code]
[hr]

Any further questions?

Orio.

Share this post


Link to post
Share on other sites

×

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.