newbie help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

newbie help

Post by dyowel »

How can i subtract time ?


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




thanks!
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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);
?>
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post by dyowel »

how do i use mktime if the values are on a variable? tnx!
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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
?>
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post by dyowel »

?? :cry:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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);
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post by dyowel »

how bout if my data is like this

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

where :
$today = date("H:i:s");
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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..
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post 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)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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"]; ?
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post 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);
      ?>
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post by dyowel »

sorry admin
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
dyowel
Forum Newbie
Posts: 8
Joined: Wed Apr 14, 2004 12:54 am

Post 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? :)
Post Reply