Php 24小时时间仅显示12小时时间
我正在开发我的PHP,因为我正在为我的脚本制作一个24小时格式,包括年、月和日的日期格式。然而,我的代码有一个小问题 在我的代码中,它显示我有24小时的时间,我可以得到12、13、14、15、16、17、18、19、20、21、22和23的时间,它显示为中午12点、下午1点、下午2点……等等。当我有24小时,我应该有12点,但我只能得到12点 凌晨1点(小时显示为1)、凌晨2点(小时显示为2)、凌晨3点(小时显示为3)的情况也一样 以下是输出:Php 24小时时间仅显示12小时时间,php,Php,我正在开发我的PHP,因为我正在为我的脚本制作一个24小时格式,包括年、月和日的日期格式。然而,我的代码有一个小问题 在我的代码中,它显示我有24小时的时间,我可以得到12、13、14、15、16、17、18、19、20、21、22和23的时间,它显示为中午12点、下午1点、下午2点……等等。当我有24小时,我应该有12点,但我只能得到12点 凌晨1点(小时显示为1)、凌晨2点(小时显示为2)、凌晨3点(小时显示为3)的情况也一样 以下是输出: <tv generator-info-nam
<tv generator-info-name="www.myscript.com/xmltv">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="20140406153000" stop="20140406183000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="20140406183000" stop="20140406210000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="20140406210000" stop="20140406120000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="20140406120000" stop="20140406123000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="20140406123000" stop="2014040610000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="2014040610000" stop="2014040613000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="2014040613000" stop="2014040623000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="2014040623000" stop="2014040630000">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
<programme channel="101 ABC FAMILY" start="2014040630000" stop="">
<title lang="en"/>
<sub-title lang="en"/>
<desc lang="en"/>
<category lang="en"/>
</programme>
</channel>
101 ABC家庭
以下是PHP:
<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
$link;
include ('simple_html_dom.php');
$html = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
$time1 = $html_two->find('span[id=time1]',0)->plaintext;
$title1 = $html_two->find('span[id=title1]',0)->plaintext;
$time1 = explode(" ", $time1);
$hoursMinutes = explode(":", $time1[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time1[1] == "PM")
{
$time1[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time1[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['start_time1'] = $time1[0];
$program_list[$count]['title1'] = $title1;
//time2
$time2 = $html_two->find('span[id=time2]', 0)->plaintext;
$title2 = $html_two->find('span[id=title2]', 0)->plaintext;
$time2 = explode(" ", $time2);
$hoursMinutes = explode(":", $time2[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time2[1] == "PM")
{
$time2[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time2[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time1'] = $time2[0];
$program_list[$count]['start_time2'] = $time2[0];
$program_list[$count]['title2'] = $title2;
//time3
$time3 = $html_two->find('span[id=time3]', 0)->plaintext;
$title3 = $html_two->find('span[id=title3]', 0)->plaintext;
$time3 = explode(" ", $time3);
$hoursMinutes = explode(":", $time3[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time3[1] == "PM")
{
$time3[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time3[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time2'] = $time3[0];
$program_list[$count]['start_time3'] = $time3[0];
$program_list[$count]['title3'] = $title3;
//time4
$time4 = $html_two->find('span[id=time4]', 0)->plaintext;
$title4 = $html_two->find('span[id=title4]', 0)->plaintext;
$time4 = explode(" ", $time4);
$hoursMinutes = explode(":", $time4[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time4[1] == "PM")
{
$time4[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time4[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time3'] = $time4[0];
$program_list[$count]['start_time4'] = $time4[0];
$program_list[$count]['title4'] = $title4;
//time5
$time5 = $html_two->find('span[id=time5]', 0)->plaintext;
$title5 = $html_two->find('span[id=title5]', 0)->plaintext;
$time5 = explode(" ", $time5);
$hoursMinutes = explode(":", $time5[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time5[1] == "PM")
{
$time5[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time5[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time4'] = $time5[0];
$program_list[$count]['start_time5'] = $time5[0];
$program_list[$count]['title5'] = $title5;
//time6
$time6 = $html_two->find('span[id=time6]', 0)->plaintext;
$title6 = $html_two->find('span[id=title6]', 0)->plaintext;
$time6 = explode(" ", $time6);
$hoursMinutes = explode(":", $time6[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time6[1] == "PM")
{
$time6[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time6[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time5'] = $time6[0];
$program_list[$count]['start_time6'] = $time6[0];
$program_list[$count]['title6'] = $title6;
//time7
$time7 = $html_two->find('span[id=time7]', 0)->plaintext;
$title7 = $html_two->find('span[id=title7]', 0)->plaintext;
$time7 = explode(" ", $time7);
$hoursMinutes = explode(":", $time7[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time7[1] == "PM")
{
$time7[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time7[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time6'] = $time7[0];
$program_list[$count]['start_time7'] = $time7[0];
$program_list[$count]['title7'] = $title7;
//time8
$time8 = $html_two->find('span[id=time8]', 0)->plaintext;
$title8 = $html_two->find('span[id=title8]', 0)->plaintext;
$time8 = explode(" ", $time8);
$hoursMinutes = explode(":", $time8[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time8[1] == "PM")
{
$time8[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time8[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time7'] = $time8[0];
$program_list[$count]['start_time8'] = $time8[0];
$program_list[$count]['title8'] = $title8;
//time9
$time9 = $html_two->find('span[id=time9]', 0)->plaintext;
$title9 = $html_two->find('span[id=title9]', 0)->plaintext;
$time9 = explode(" ", $time9);
$hoursMinutes = explode(":", $time9[0]);
$hours = $hoursMinutes[0];
$minutes = $hoursMinutes[1];
if($time9[1] == "PM")
{
$time9[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
}
else
{
$time9[0] = date("Ymd") . $hours . $minutes . "00";
}
$program_list[$count]['end_time8'] = $time9[0];
$program_list[$count]['start_time9'] = $time9[0];
$xml .= "<channel id='" . $my_id. " " . $channel . "'>";
$xml .= "<display-name>" . $my_id. " " . $channel;
$xml .= "</display-name>";
$xml .= "<programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time1'] . "' stop='" . $program_list[$i]['end_time1'] . "'>";
$xml .= '<title lang="en">';
$xml .= '</title>';
$xml .= '<sub-title lang="en">';
$xml .= '</sub-title>';
$xml .= '<desc lang="en">';
$xml .= '</desc>';
$xml .= '<category lang="en">';
$xml .= '</category>';
$xml .= '</programme>';
这段代码很难读懂,但您已经
((int)($hours) + 12)
我会用这样的东西来代替
getPaddedHour((int)$hours, $time9[1]);
并将函数定义为
function getPaddedHour($hour, $meridian) {
$ret = $hour;
if ($meridian == 'AM' && $ret == 12) {
$ret = 0;
} else if ($meridian == 'PM' && $ret != 12) {
$ret += 11;
}
if ($ret < 10) {
$ret = '0'.$ret;
}
return $ret;
}
然后,后来
if ($start_time !== FALSE) {
$program_list[$count]['start_time1'] = date('YmdHis', $time);
} else {
$program_list[$count]['start_time1'] = 'Not Available';
}
if/else语句是b/c如果您期望从输入中得到的信息没有传递,那么您需要对此进行说明,strotime将返回false。Use DateTime()当使用日期时,AW时间和日期操纵是一个糟糕的想法。你真的需要将其简化为一个更简明的示例,以便任何人都能提供帮助。@johncode和Dagon谢谢你的建议,你能告诉我为了获得24小时时钟,我需要做哪些更改吗?非常感谢,非常抱歉。我不认为我应该在哪里输入代码。看来我现在拿到了,我想我知道它在哪里了。我不知道应该在哪里输入代码。我想我找到了。所以我已经记下了你的答案。:)@Jeremey Miller对此非常感谢,那么我用什么正确的代码来替换getPaddedHour((int)$hours,$time9[1])代码>使用$time1[0]=日期(“Ymd”)。((整数)($hours)+12)$分钟"00";代码>?我明白,但你能给我一个你收到的实际数据的例子吗?然后我可以建议一个合适的替代方案。我如何才能为您提供我收到的实际数据的输出?在您对其进行任何更改之前,我需要将echo放入哪一行以获取输出?$time1
。我需要将echo$time1放入哪一行?
if ($start_time !== FALSE) {
$program_list[$count]['start_time1'] = date('YmdHis', $time);
} else {
$program_list[$count]['start_time1'] = 'Not Available';
}