简体   繁体   English

从PHP中没有AM和PM的12小时中减去24小时

[英]Deducing 24 hour time from a 12 hour time without AM and PM in PHP

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. 我意识到,如果我不使用这种粗略的方法来访问数据,但是没有可用的API,这会容易得多,而且这比我希望公开发布的东西更多的是学习项目。

Anyhow, the schedule I'm reading from displays times in 12-hour format but without AM/PM (so for example, just 9:43). 无论如何,我从中读取的时间表以12小时格式显示时间,但没有AM / PM(例如,只有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. 我将时间作为近似的unix时间戳存储在数据库中,这意味着我需要我的脚本才能弄清楚时间是上午还是下午。

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. 因此,在脚本运行时的上午9点,可以列出即将到来的下午2点的火车,如果早上7点的火车没有按时离开的话,它可能仍在董事会上。

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? 有什么好的方法来解决这个问题?

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. 如果您知道要刮取页面的时间(应该),并且知道列出的时间(显然可以), 并且您知道该时间是页面访问的-2到+6(例如,您刮取页面的时间)页)...我看不到问题出在哪里。看来您已掌握了所有需要的信息。

I scrape a page at 11:30 (AM). 我在11:30(AM)刮了一页。 There is a departure listed for 2:15. 列出的出发时间是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). 好吧,当在2:15 AM和2:15 PM之间进行选择时,只有11:30(AM)之后不到6小时的两个时间之一。 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). 如果我看到10:30的条目,我会知道它必须是“一个小时前”,因为将来的11小时到达时间不会列出(根据您的解释)。

Or am I missing something? 还是我错过了什么?

Traditionally, train schedules distinguish am and pm with lightface and boldface times. 传统上,火车时刻表将上午和下午区分为亮面和黑体时间。 As best I can remember, pm is always bold. 据我所记得,pm总是大胆的。 If that's the case for your source, just keep track if the text is inside <b> or <strong>. 如果是这种情况,请跟踪文本是否在<b>或<strong>之内。

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. 好的,我忘记了该脚本会在火车上提前出现的情况下运行以初始化火车,因此“ 2小时前”不是问题。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM