我需要一些设置日期格式的帮助。
我仍然试图找出如何设置格式到第二天,第二天和第三天的日期,当我计算时间。
示例:我从今天开始设置日期格式,时间是上午10:00,上午11:00,中午12:00,格式为20140424100000,20140424110000,20140424120000等等,直到我对12做出反应:00 AM,我想将格式设置为第二天20140425000000,20140425003000,20140425020000,20140425023000,直到我对下一个上午12:00的反应我要设置新格式到第二天20140426000000,20140426003000,20140426010000
我的问题是当我从我的脚本解析时间字符串列表时,我如何计算出日期和时间的格式?
这是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
答案 0 :(得分:1)
编辑:
正如您现在所说,我们正在将时间转换为您的格式。基于第一个元素是今天。
关键是要检查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
以获取您的确切格式。