接收的字符串,一般有下面几种可能的格式:$s1 = 'December 23–26, 2010';//某年同一个月的两个日期
$s2 = 'April 28–May 1, 2011';//某年两个月的两个日期
$s3 = 'December 30–January 2, 2011';//跨年的两个月的两个日期,第一个日期是2010年的接收这个字符串,求一个函数处理,将其转换成对应两个时间段的unix时间戳格式
$s2 = 'April 28–May 1, 2011';//某年两个月的两个日期
$s3 = 'December 30–January 2, 2011';//跨年的两个月的两个日期,第一个日期是2010年的接收这个字符串,求一个函数处理,将其转换成对应两个时间段的unix时间戳格式
$s1_2 = strtotime(date('Y-m-d','2010-12-26'));
$s2_1 = strtotime(date('Y-m-d','2011-04-28'));
$s2_2 = strtotime(date('Y-m-d','2011-05-01'));
$s3_1 = strtotime(date('Y-m-d','2010-12-30'));
$s3_2 = strtotime(date('Y-m-d','2011-01-02'));楼上的,问题是接收的字符串日期是英文的,怎么转换呢?
$test=strtotime('December 23 2010');echo date('Y-m-d G:i:S',$test);
$s1 = 'December 23–26, 2010';//某年同一个月的两个日期
$s2 = 'April 28–May 1, 2011';//某年两个月的两个日期
$s3 = 'December 30–January 2, 2011';//跨年的两个月的两个日期,第一个日期是2010年的function str2time($str) {
$reg = '/([a-z]+)\s(\d+)–([a-z]+)?\s?(\d+), (\d+)/i';
$matches = array();
$ret = array();
if(preg_match($reg, $str, $matches)) {
list(, $m1, $d1, $m2, $d2, $y2) = $matches;
$m2 = $m2 ? $m2 : $m1;
$y1 = date('m', strtotime($m1)) > date('m', strtotime($m2)) ? $y2 - 1 : $y2;
$ret[] = strtotime($y1.'-'.$m1.'-'.$d1);
$ret[] = strtotime($y2.'-'.$m2.'-'.$d2);
} else {
$ret[] = strtotime($str);
}
return $ret;
}print_r(str2time($s1));