Jump to content

date difference ...


sun14php

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



Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
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;
Link to comment
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.
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.