使用 php 减去特定行上的时间

发布于 2024-09-10 04:23:23 字数 1319 浏览 12 评论 0原文

使用 php 我想减去/减去两次;每次在一个单独的文档中,然后让 php 将结果(减去两次后的结果)写入另一个单个文本文档或没有 HTML 标签、只有文本的 html 文档中。减去的两次时间将确定某个玩家玩该脚本所用的游戏的时间。另外,当我说脚本必须减去两次时,这实际上是针对一名玩家的,因此总的来说应该减去两次以上;取决于实际玩游戏的人数。

如果您看一下下面的内容,这是一个文本文件,其中包含玩家的用户名以及玩家的当地时间,后跟与本例无关的其他数字:

该文本文件名为“begin.txt” ”:

“AULLAH1”“2010年1月7日15:28”“55621454”“123456”“123456.00”

“用户名”“7/3/2010 6:21 PM”“55621454”“123456”“123456.00”

“AULLAH1”“2010年7月7日15:05”“55621454”“189450”“123456.00”

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

有两个“AULLAH1”用户名条目,因为该玩家已经玩过该游戏两次。第一行的下一个条目是“01/07/2010 15:28”,这是人“AULLAH1”的日期和当前时间。日期和时间之后的其他数据不相关,因此可以忽略。

在另一个文本文档中,标题为“end.txt”的是以下数据,其格式与之前的文本文件相同:

“AULLAH1”“2010年1月7日15:54”“55621454”“123456”“123456.00”

“用户名”“7/3/2010 6:49 PM”“55621454”“123456”“123456.00”

“AULLAH1”“2010年7月7日15:32”“55621454”“189450”“123456.00”

此文本文件中的不同之处在于,没有标题为“SUPREMEGAMER”的玩家,因为该玩家未能完成游戏。此外,对于那些成功完成游戏的用户来说,时间也有所不同。玩家“AULLAH1”也被列出两次,因为该玩家已完成该游戏两次。

我希望 php 脚本在 end.txt 文件中搜索用户名“AULLAH1”的最后一个条目,然后再次抓取整行,从 begin.txt 中搜索“AULLAH1”的最后一个条目,然后减去时间。减去后,我希望将用户名和时间写入另一个文本文档或没有 HTML 标签、只有文本的 html 文档中(如前所述)。因此,这只会显示用户“AULLAH1”的游戏时间,因此,我希望对 end.txt 文档中的每个用户名执行此过程。

感谢所有帮助,我期待您的回复;谢谢。 :) 如果我没有清楚地解释任何内容和/或您希望我更详细地解释,请回复。 :)

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. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-09-17 04:23:23

我不打算在这里讨论文本文件搜索。假设您最终得到了开始时间和结束时间。区分两者之间的差异非常容易。

(值得注意的是,我认为你的时间颠倒了。“结束”文件中的时间总是低于“开始”文件中的时间,这可能是不对的。此外,你似乎将日期存储在 DD 中/MM/YYYY 格式,我的示例可能无法正确解析,因为它对于 13 号之前的任何一天都可能不明确,您可能希望将它们存储为 YYYY-MM-DD 格式,即完全明确。)

这是方法 1,使用普通的旧 unix 时间戳。

// 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.";

这是方法 2,使用 PHP 新的 DateTimeDateInterval 类。这仅适用于 PHP 5.3 或更高版本。

// 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.');

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.');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文