接收的字符串,一般有下面几种可能的格式:$s1 = 'December 23–26, 2010';//某年同一个月的两个日期
$s2 = 'April 28–May 1, 2011';//某年两个月的两个日期
$s3 = 'December 30–January 2, 2011';//跨年的两个月的两个日期,第一个日期是2010年的接收这个字符串,求一个函数处理,将其转换成对应两个时间段的unix时间戳格式

解决方案 »

  1.   

     正则组合出 2个时间 然后 strtotime.
      

  2.   

    最后得到的是$s1_1 = strtotime(date('Y-m-d','2010-12-23'));
    $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'));楼上的,问题是接收的字符串日期是英文的,怎么转换呢?
      

  3.   

    有啥问题吗?英文的不需要考虑啊
    $test=strtotime('December 23 2010');echo date('Y-m-d G:i:S',$test);
      

  4.   

    你需要下載PHP函數手冊,好好看看strtotime的講解
      

  5.   

    本帖最后由 xuzuning 于 2011-05-10 11:10:33 编辑
      

  6.   

    <?php    
    $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));