PHP中没有AM和PM的情况下从12小时时间推算24小时时间

发布于 2024-10-01 03:07:03 字数 497 浏览 2 评论 0原文

我正在开发一个项目,该项目可以从网络上发布的火车时刻表中筛选出出发时间列表。我意识到,如果我不使用如此粗略的方法来访问数据,那么这会容易得多,但没有可用的 API,而且它更像是一个学习项目,而不是我期望公开发布的那种东西。

无论如何,我正在读取的时间表以 12 小时格式显示时间,但没有 AM/PM(例如,仅 9:43)。我将时间作为近似的 unix 时间戳存储在数据库中,这意味着我需要我的脚本能够确定时间是上午还是下午。

我从列表中抓取的数据可能是两小时前到未来六个小时之间的时间。因此,在上午 9 点脚本运行时,可以列出即将到来的下午 2 点的火车,并且如果早上 7 点的火车没有准时出发,则仍然可以在板上。

我编写了一个带有两个参数的函数——要评估的时间,以及基于“猜测”的当前系统时间(我意识到我可以让该函数获取时间本身,但我试图编写一个单元测试那非常失败,这就是我这样做的原因)。我想把它发布在这里,但它并没有真正起作用,我想从你们好人的一些指导或技巧开始。

有人可以帮我吗?有什么好方法来解决这个问题?

I'm working on a project that screen scrapes a list of departure times from a train schedule posted on the web. I realize this would be a lot easier if I wasn't using such a crude method to access the data but there's no API available, and it's more of a learning project than the kind of thing I expect to release publicly.

Anyhow, the schedule I'm reading from displays times in 12-hour format but without AM/PM (so for example, just 9:43). I'm storing the time in a database as an approximate unix timestamp, which means I need my script to be able to figure out if a time is AM or PM.

The data I'm scraping from lists times that are, potentially, between two hours ago and six hours in the future. So at 9am when the script runs, an upcoming 2pm train could be listed, and a 7am train could still be on the board if it didn't leave on time.

I wrote a function that takes two parameters -- the hour to be evaluated, and the current system hour to base the "guess" on (I realize I could have the function get the time itself, but I was trying to write a unit test that failed horribly, that's why I did that). I'd post it here but it doesn't really work, and I'd like to start fresh with some guidance or tips from you fine folks.

Can anyone help me out? What a good way to approach this?

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

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

发布评论

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

评论(3

悲念泪 2024-10-08 03:07:03

如果您知道抓取页面的时间(您应该),并且您知道列出的时间(显然您知道),并且您知道页面访问时间为 -2 到 +6(例如,您抓取页面的时间)...我看不出问题出在哪里。看起来您拥有所需的所有信息。

我在上午 11:30 抓取了一页。列出的出发时间为 2:15。那么,当在 2:15AM 和 2:15PM 之间进行选择时,两者中只有一个距离 11:30(AM) 不到 6 小时。如果我看到 10:30 的条目,我就知道它必须是“一小时前”,因为未来 11 小时到达的条目不会被列出(根据您的解释)。

或者我错过了什么?

If you know what time you scraped the page (you should), and you know the time listed (clearly you do), and you know that the times are -2 to +6 of the page access (eg, the time you scraped the page)... I'm failing to see where the trouble comes in. It seems like you have all the information you need.

I scrape a page at 11:30 (AM). There is a departure listed for 2:15. Well, when choosing between 2:15AM and 2:15PM, there's only one of the two that's less than 6 hours after 11:30(AM). If I saw an entry for 10:30, I'd know it had to be "an hour ago" because an arrival 11 hours in the future wouldn't be listed (per your explanation).

Or am I missing something?

分开我的手 2024-10-08 03:07:03

传统上,火车时刻表用浅色和粗体时间来区分上午和下午。据我所知,pm 总是粗体。如果您的源代码属于这种情况,只需跟踪文本是否位于 内即可。或<强>。

Traditionally, train schedules distinguish a.m. and p.m. with lightface and boldface times. As best I can remember, p.m. is always bold. If that's the case for your source, just keep track if the text is inside <b> or <strong>.

有木有妳兜一样 2024-10-08 03:07:03

好吧,我忘记了这个脚本运行来初始化火车,因为火车提前几个小时出现在板上,所以“2 小时前”的事情不是问题。这是我想出的,它似乎有效:

 function convertTime($input, $currentHour) {
    if ($currentHour >= 8 && $currentHour < 12 && $input < 8) {
        $input += 12;
    }
    if ($currentHour > 12 && $currentHour < 20 && $input < 12) {
        $input += 12;
    }
    if ($currentHour > 20 && $currentHour < 24 && $input > 8) {
        $input +=12;
    }
$return $input;
}

OK, I forgot that this script runs to initialize trains as the appear on the board hours in advance, so the "2 hours ago" thing is not an issue. Here's what I came up with, it seems to be working:

 function convertTime($input, $currentHour) {
    if ($currentHour >= 8 && $currentHour < 12 && $input < 8) {
        $input += 12;
    }
    if ($currentHour > 12 && $currentHour < 20 && $input < 12) {
        $input += 12;
    }
    if ($currentHour > 20 && $currentHour < 24 && $input > 8) {
        $input +=12;
    }
$return $input;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文