我正在处理我的PHP,我正在显示月份,月份,日期和时间格式。我的代码有点问题。
在我的代码中,它显示它只显示我希望获得24小时格式的12小时格式。当我有时间时,例如:上午12点。我想把小时显示为00,但我只得到12小时。同样的事情是凌晨1点(小时显示为1),凌晨2点(小时显示为2),凌晨3点(小时显示为3)。
这是输出:
<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>
这是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;
$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>';
正如您在开始时间标记上看到的那样,例如:20140406183000。2014年是年份,04是月份,06是月份的日子,18是小时,30是分钟,00是秒。如果你查看时间,你会知道什么时间是正确的,什么时间是不正确的。
你可以看到,我有一些不正确的时间。不正确的时间是早上12点(假设是00),凌晨1点(假设是01),凌晨2点(假设是02)等等。
您能告诉我解决问题需要做些什么吗?
答案 0 :(得分:1)
使用strtotime()
将正常的英文文本日期时间转换为时间戳,将date()
转换为您想要的任何格式。
$datetime = "03-05-2000 03:07:34 pm";
$timestamp = str_to_time($datetime);
$date = date('d M, Y H:i:s',$timestamp); //03 May, 2000 15:07:34
答案 1 :(得分:1)
在您的代码中,您可以这样做:
$time_old_format = $html_two->find('span[id=time1]',0)->plaintext;
$time_new_format = date("YmdHis", strtotime($time_old_format));
这将解析,例如字符串12:30 AM
到20140407003000
。 (当然取决于当天。)