如何用小时来计算格式


how to work out the format with hours

我需要一些帮助来设置日期格式。

我仍在努力找出如何将格式设置到第二天、第二天和第三天,当我计算时间时。

示例:我从今天开始设置日期格式,时间为上午10:00、上午11:00、下午12:00,格式为201404244100000、201404241100000、201400424120000等等,直到我对上午12:00做出反应时,我想将格式设置为第二天201404255000000、201404250003000、201404255020000,20140425023000直到我对下一个12:00 AM做出反应时,我想将新格式设置为第二天201404260000002014042600300020140426010000。

我的问题是,当我从脚本中解析时间字符串列表时,如何计算出日期和时间的格式?

这是PHP:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
include ('simple_html_dom.php');
foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];
  $html_two = file_get_html("http://www.mysite.com/myscript.php?getime);
  //time1
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];
  $time1 = explode(" ", $time1);
  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  //time2
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $hoursMinutes = explode(":", $time2[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];
  $time2 = explode(" ", $time2);
  $hoursMinutes = explode(":", $time2[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  //time3
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $hoursMinutes = explode(":", $time3[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];
  $time3 = explode(" ", $time3);
  $hoursMinutes = explode(":", $time3[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];
?>

这是时间字符串:

10:00 AM
11:00 AM
12:00 PM
12:30 PM
2:00 PM
2:30 PM
9:00 PM
11:00 PM
12:00 AM
12:30 AM
1:00 AM
1:30 AM
2:00 AM
11:00 PM
12:00 AM
12:30 AM
1:00 AM

编辑:例如,请参阅我想要实现的目标:

10:00 AM - 20140424100000
11:00 AM - 20140424110000
12:00 PM - 20140424120000
12:30 PM - 20140424123000
2:00 PM - 20140424140000 
2:30 PM - 20140424143000
9:00 PM - 20140424210000
11:00 PM - 20140424230000 
12:00 AM - 20140425000000
12:30 AM - 20140425003000
1:00 AM - 20140425010000
1:30 AM - 20140425013000
2:00 AM - 20140425020000
11:00 PM - 20140425230000
12:00 AM - 20140426000000
12:30 AM - 20140426003000
1:00 AM - 20140426010000

编辑:

正如您现在所要求的,我们正在将时间转换为您的格式。基于第一个元素是今天。

关键是检查PM-AM之间的转换是否意味着一天。

// Lets assume that we have this array of time strings. 
$array = array(
    "10:00 AM",
    "11:00 AM",
    "12:00 PM",
    "12:30 PM",
    "2:00 PM",
    "2:30 PM",
    "9:00 PM",
    "11:00 PM",
    "12:00 AM",
    "12:30 AM",
    "1:00 AM",
    "1:30 AM",
    "2:00 AM",
    "11:00 PM",
    "12:00 AM",
    "12:30 AM",
    "1:00 AM",
);
// Save the output format
$DATE_FORMAT_STRING = "Y m d H i s";
// function to get the state
function getState($string){
    $ex = explode(" ",$string);
    return $ex[1];
}
// GET the current STAGE
$current_state = getState($array[0]);
$offset = 0;
$values = array();
foreach($array as $time){
    // Get the item state.  
    $this_state = getState($time);
    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM"){
        $offset++;
    }
    $this_unix = strtotime($time) + (60 * 60 * 24 * $offset);
    $values[] = date($DATE_FORMAT_STRING, $this_unix);

    $current_state = $this_state;
}

这将输出:

2014 04 24 10 00 00
2014 04 24 11 00 00
2014 04 24 12 00 00
2014 04 24 12 30 00
2014 04 24 14 00 00
2014 04 24 14 30 00
2014 04 24 21 00 00
2014 04 24 23 00 00
2014 04 25 00 00 00
2014 04 25 00 30 00
2014 04 25 01 00 00
2014 04 25 01 30 00
2014 04 25 02 00 00
2014 04 25 23 00 00
2014 04 26 00 00 00
2014 04 26 00 30 00
2014 04 26 01 00 00

Y m d H i s更改为YmdHis以获得确切的格式。