24小时的时间只显示12小时的时间

时间:2014-04-06 20:28:01

标签: php

我正在处理我的PHP,因为我为我的脚本制作了24小时格式,包括年,月和日日期格式。但是,我的代码有点问题。

在我的代码中,它显示我有24小时的时间,我可以得到12,13,14,15,16,17,18,19,20,21,22和23的小时,它显示为中午12点,下午1点,下午2点等。当我有24个小时,我应该在早上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;

   //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>';

正如您在开始时间标记上看到的那样,例如:20140406183000。2014年是年份,04是月份,06是月份的日子,18是小时,30是分钟,00是秒。如果你查看时间,你会知道什么时间是正确的,什么时间是不正确的。你可以看到,我有一些时间没有设置正确。不正确的时间是早上12点(假设是00),凌晨1点(假设是01),凌晨2点(假设是02)等等。

您能告诉我解决问题需要做些什么吗?

1 个答案:

答案 0 :(得分:0)

这段代码很难阅读,但你有

的地方
((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;
}

但我完全赞同这些评论者。试图自己解析日期/时间是一件痛苦而且不可靠的事情,这就是我们为这些语言使用函数的原因。

编辑:假设$time2的格式为h:mm a(例如5:30 PM),那么strtotime可用于转换为可靠的当前日期的格式(您在帖子中使用的格式)。

例如:

$start_time = strtotime($html_two->find('span[id=time1]',0)->plaintext);

然后,稍后......

if ($start_time !== FALSE) {
  $program_list[$count]['start_time1'] = date('YmdHis', $time);
} else {
  $program_list[$count]['start_time1'] = 'Not Available';
}

if / else语句是b / c,如果你没有传递你对输入的期望,那么你将需要考虑它并且strtotime将返回false。