php - 24 hours time are only showing 12 hours time -
i'm working on php i'm making 24 hours format script including year, month , day date format. however, have got little problem code.
in code, shows have 24 hours time can hours 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 , 23 shows 12pm, 1pm, 2pm...etc. when have 24 hours supposed have 00 12am, can 12.
the same things goes 1am (hour show 1), 2am (hour show 2), 3am (hour show 3).
here output:
<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>
here 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>';
as can see on start time tag, e.g: 20140406183000. 2014 year, 04 month, 06 day of month, 18 hour, 30 mins , 00 seconds. if @ hours, know hours correct , hours not correct. can see, have got of hours not set correct. hours not correct 12am (supposed 00), 1am (supposed 01), 2am (supposed 02) , on.
can please tell me need fix problem?
this code rather difficult read, have
((int)($hours) + 12)
i replace this
getpaddedhour((int)$hours, $time9[1]);
and define function as
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; }
but wholly agree commenters. trying parse date/time on own pain , unreliable, that's why there functions made use in language.
edit: assuming $time2
of form h:mm a
(e.g. 5:30 pm
), strtotime
can used convert reliable format current date (which you're using in post).
for example:
$start_time = strtotime($html_two->find('span[id=time1]',0)->plaintext);
then, later on...
if ($start_time !== false) { $program_list[$count]['start_time1'] = date('ymdhis', $time); } else { $program_list[$count]['start_time1'] = 'not available'; }
the if/else statement b/c if expecting input not delivered, need account , strtotime return false.
Comments
Post a Comment