views:

48

answers:

1

Using php I'd like to subtract/minus two times; each time within a separate document and thereafter have php write the result (Result after subtracting the two times) within another single text document or within a html document with no HTML tags, just text. The two times subtracted will determine how long a certain player has played the game on which this script will be used for. Also, when I state the script must subtract two times, that is actually for one player, therefore in overall there should be more than two times subtracted; depending on how many people actually play the game.

If you take a look at the following below, this is a text file containing a player's user name as well as the local time for a player, followed by other numbers which are irrelevant within this case:

The text file is called "begin.txt":

"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"

"USERNAME" "7/3/2010 6:21 PM" "55621454" "123456" "123456.00"

"AULLAH1" "07/07/2010 15:05 " "55621454" "189450" "123456.00"

"SUPREMEGAMER" "7/8/2010 6:42 PM" "55621454" "123456" "123456.00"

There are two "AULLAH1" user name entries, as this player has played the game twice. The next entry on the first line is "01/07/2010 15:28 " which is the date and current time for the person "AULLAH1". The other data after the date and time are irrelevant, therefore they can be ignored.

Within another text document, titled "end.txt" is the following data, which is in the same format as the previous text file:

"AULLAH1" "01/07/2010 15:54 " "55621454" "123456" "123456.00"

"USERNAME" "7/3/2010 6:49 PM" "55621454" "123456" "123456.00"

"AULLAH1" "07/07/2010 15:32 " "55621454" "189450" "123456.00"

The difference within this text file is that there is no player titled "SUPREMEGAMER" as this player didn't manage to finish the game. Also, the time is different for those users who managed to finish the game. Player "AULLAH1" is also listed twice as this player has finished this game twice.

I'd like the php script to search the end.txt file for the last entry of user name "AULLAH1" and grab the entire line thereafter again, search for the last entry of "AULLAH1" from begin.txt and then subtract the times. Once it's subtracted, I'd like the user name as well as time to be written within another text document or within a html document with no HTML tags, just text (as stated earlier). So, this will only show the time played for the user "AULLAH1" therefore, I'd like this process to be done for each and every user name within the end.txt document.

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

+1  A: 

I'm not going to address the text file searching here. Let's say that eventually you get the start and end times together. Getting the difference between the two is pretty easy.

(Of note, I think you have your times reversed. The times in the "end" file are always lower than the times in the "begin" file, and that's probably not right. Additionally, you seem to be storing the dates in DD/MM/YYYY format, which my examples might not parse correctly because it can be ambiguous for any day prior to the 13th. You may want to store them in YYYY-MM-DD format instead, which is completely unambiguous.)

Here's way 1, using plain old unix timestamps.

// First we have our times in a human readable format. 
// Let's make them into unix timestamps
$start_time = strtotime('01/07/2010 15:28');
$end_time = strtotime('01/07/2010 15:54');
$difference_in_seconds = $end_time - $start_time;
// If you need the difference in minutes or hours, you'll need to 
// do the math yourself.
echo "That turn took {$difference_in_seconds} seconds to complete.";

Here's way 2, using PHP's new DateTime and DateInterval classes. This will only work in PHP 5.3 or better.

// We have our times in human readable format.
// Let's make them into DateTimes
$start_time = new DateTime('01/07/2010 15:28');
$end_time = new DateTime('01/07/2010 15:54');
$interval = $end_time->diff($start_time, true);
echo "That turn took " . $interval->format('%h hours, %i minutes and %s seconds.');
Charles
Thank you for your reply Charles, I really appreciate it. :) Sorry, you were correct I had my begin.txt and end.txt files mixed up, anyhow I've now updated that. I suppose I asked for too much in my first question and I like the script you've provided and will be using it. Thank you and I do find it useful. :)
AUllah1