Page 1 of 1

newbie help

Posted: Wed Apr 14, 2004 12:54 am
by dyowel
How can i subtract time ?


ex. 23:30:15 - 18:15:35




thanks!

Posted: Wed Apr 14, 2004 1:25 am
by RadixDev
Use UNIX time expression
[php_man]mktime()[/php_man] function to create the UNIX time number more on that on the manual:-

Code: Select all

<?php
$date1 = mktime(23,30,15,0,0,0);
$date2 = mktime(18,15,35);
$date3 = $date1 - $date2;
?>
Format using the [php_man]date()[/php_man] function:-

Code: Select all

<?php
$date3 = date("H:i:s" ,$date3);
?>

Posted: Wed Apr 14, 2004 1:43 am
by dyowel
how do i use mktime if the values are on a variable? tnx!

Posted: Wed Apr 14, 2004 1:45 am
by RadixDev
Well if you have the values as you said in a variable. Split the values using explode().

Code: Select all

<?php
$time = "23:30:15"; // assuming that it is in hh:mm:ss format
$time = explode(":", $time);
// $time[0] = hour
// $time[1] = minute
// $time[2] = seconds
?>

Posted: Wed Apr 14, 2004 3:58 am
by dyowel
?? :cry:

Posted: Wed Apr 14, 2004 4:04 am
by markl999
Something like:

Code: Select all

$time1 = '23:30:15';
$time2 = '18:15:35';
list($hour, $minute, $second) = explode(':', $time1);
$t_time1 = mktime($hour, $minute, $second);
list($hour, $minute, $second) = explode(':', $time2);
$t_time2 = mktime($hour, $minute, $second);
echo date('h:i:s', $t_time1 - $t_time2);

Posted: Wed Apr 14, 2004 4:12 am
by dyowel
how bout if my data is like this

$time_start = $row["timein"];
$time_end = $today;

where :
$today = date("H:i:s");

Posted: Wed Apr 14, 2004 4:16 am
by markl999
As long as the times are in 23:30:15 format then it shouldn't matter where they are coming from, a database, file, hardcoded etc..

Posted: Wed Apr 14, 2004 4:25 am
by dyowel
why is it i always got a wrong result.

data are :

time in : 17:22:04
time out : 17:22:06
result : 01:22:06


my code

Code: Select all

$today = date("H:i:s"); 
      include "./connect.php";			  
      $connect = mysql_pconnect($dbhost,$dbusername,$dbuserpassword);
      $result = mysql_db_query($dbname,"SELECT * from asian_field where  code = '$code'"); 
	  
      $time1 = $row["timein"];
      $time2 = $today;
      list($hour, $minute, $second) = explode(':', $time1); 
      $t_time1 = mktime($hour, $minute, $second); 
      list($hour, $minute, $second) = explode(':', $time2); 
      $t_time2 = mktime($hour, $minute, $second); 
      $total = date('h:i:s', $t_time2 - $t_time1); 

      $query = "update asian_field set timeout = '$today', elapse_time = '$total'  where code = '$code'";
      $result = mysql_db_query($dbname,$query);
PLEASE USE BBCODE TO HIGHLIGHT YOUR PHP...THANKS (BECH100)

Posted: Wed Apr 14, 2004 4:30 am
by markl999
$time1 = $row["timein"];
You never set $row, maybe you are missing a :
$row = mysql_fetch_assoc($result);
before you do $time1 = $row["timein"]; ?

Posted: Wed Apr 14, 2004 4:40 am
by dyowel
MOD MSG: PLEASE USE THE BBCODE TO HIGHLIGHT YOUR PHP! THANKS

result :

in : 17:39:14
out : 17:39:15
result : 08:00:01

Code: Select all

<?
	  $today = date("H:i:s"); 
	  include "./connect.php";			  
      $connect = mysql_pconnect($dbhost,$dbusername,$dbuserpassword);
      $result = mysql_db_query($dbname,"SELECT * from asian_field where  code = '$code'"); 
	  $row = mysql_fetch_array($result);
	  
      $time1 = $row["timein"];
	  $time2 = $today;
      list($hour, $minute, $second) = explode(':', $time1); 
      $t_time1 = mktime($hour, $minute, $second); 
      list($hour, $minute, $second) = explode(':', $time2); 
      $t_time2 = mktime($hour, $minute, $second); 
      $total = date('h:i:s', $t_time2 - $t_time1); 

      $query = "update asian_field set timeout = '$today', elapse_time = '$total'  where code = '$code'";
      $result = mysql_db_query($dbname,$query);
      ?>

Posted: Wed Apr 14, 2004 4:51 am
by dyowel
sorry admin

Posted: Wed Apr 14, 2004 4:55 am
by JayBird
dyowel wrote:sorry admin
Don't worry :)

Try echoing $time1 and $time2 to make sure they are the times you expect.

Post back saying what they were.

Mark

Posted: Wed Apr 14, 2004 8:52 pm
by dyowel
Bech100 wrote:
dyowel wrote:sorry admin
Don't worry :)

Try echoing $time1 and $time2 to make sure they are the times you expect.

Post back saying what they were.

Mark



Hi guys its me again.. i think i know waht the problem is. Its the conversion of seconds into time. I have echoed the $time1, $time2 and the result.

data :

time1 : 09:51:33
time2 : 09:51:35

result : 2

How can i convert the seconds into time format? :)