Elapsed time in php

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
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

Elapsed time in php

Post by laknal »

Hi,

I am trying to get time differene between submit time of previous record and start time time next record.

Please see below code:

Code: Select all

<?php
	if(!empty($prevTime)) 
               {
                      $diff = $prevTime - strtotime($row_rsarticle['created_time']);
	       echo date("Y-m-d H:i:s", $diff);
	}
	$prevTime = strtotime($row_rsarticle['time_submit']);
?>
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

My mind reading skills aren't so hot, can you explain the problem you're having?
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

Elapsed time in php

Post by laknal »

I am trying to get the elaspsed time between time submit of first records and start of second records.

I am getting date field errors. Example:

1. End record for first record is 10.55.00
2. Start time for second record is 11.55.00

Desired result is 1.00.00.

But I am getting "Warning: date function invalid" error.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could make it work with a datetime field, but may I suggest you use a unix timestamp instead. It'll make your life much easier.
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

Elapsed time in php

Post by laknal »

Hi,

Both fields are timestamp in MYSQL database.

Is there a function in php to get previous recordset datefield? May be I can do compute that datafield to current recordset datafield.


Thanks.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

A period of time (eg 2 hours) is not a date. So you can't use the date functions with it.

If you have two timestamps however you can simply subtract the smaller one from the bigger one to get the number of seconds difference, and once you have that it's pretty easy to convert seconds into other time values. For example, divide by 60 for minutes, by 3600 for hours, by 86400 for days and so on.

You'll need to do a bit of coding if you want a mixed format (eg hours, minutes and seconds).
Post Reply