在php中从同一个字符串中解析多个日期

时间:2016-01-27 17:40:59

标签: php

我进行了技术测试,我必须做的一件事是一个输入有两个日期的程序,并且必须在这两个日期之间输出5个星期日的月份。字符串$dates在同一行中有两个日期 示例:juny 2014 october 2014 我需要用这两个日期来做事。

在这一点上,我想要做两个单独的字符串来处理每个日期,如下所示:

$arrayDates = split(" ",$dates);  

然后:

$firstDate = $dates[0].$dates[1];
$secondDate = $dates[2].$dates[3];

但我看到这种方式做得太明显而不是动态。有人知道更好或更优雅/适当的方式吗?

编辑:这是完整的代码

<?php
date_default_timezone_set('UTC');

//output month with five sundays

foreach(file($argv[1]) as $date){

    $totalDays=prepareDateArray($date);
    calculateSundays($totalDays,$date);
}

function prepareDateArray(&$date){
$regexPattern = "/([a-z]+ [0-9]{4})/i";
$matchCount = preg_match($regexPattern, $date, $matches);
if($matchCount == 0)return;
$date = preg_replace("#  +#",' ',$date);
$date = split(' ',$date);

$totalDays = parseDate($date);
return $totalDays;
}

function parseDate(&$date){

    $nIndex = count($date);
    if($nIndex%2==0){
        //Add first day of the month to first Date
        $date[0] = '01-'.$date[0].'-'.$date[1];
        //Get lastMonthDay
        $lastMonthDay = date('t', strtotime($date[2].'-'.$date[3])); 
        //Add last day of the month to secondDate
        $date[1] = $lastMonthDay.'-'.$date[2].'-'.$date[3];
        //Calculates distance between dates in days
        $firstDate = strtotime($date[0]);
        $secondDate = strtotime($date[1]);
        if($firstDate>$secondDate)exit('First date must be earlier than second date');
        $datediff = $secondDate - $firstDate;
        unset($date[2],$date[3]);

        return $datediff/(60*60*24);
    }else{
        exit('Insert valid input');
    }
}

function calculateSundays($totalDays,$tempDate){

    $sundays=0;
    $output=0;
    for($day=1;$day<=$totalDays;$day++)
    {
        $dayOfWeek = date('w', strtotime($tempDate[0].' + '.$day.' days'));
        if($dayOfWeek == 0)$sundays++;
        if($sundays == 5){
            $output++;
            $sundays = 0;
        }
    }
    if($output == 0)print("Wrong date \n");
    else print($output."\n");
}
?>

2 个答案:

答案 0 :(得分:1)

您可以为此操作创建一个简单的正则表达式,可以查看下面的代码;

<?PHP
    header('Content-Type: text/plain; charset=utf8');
    $regexPattern = "/([a-z]+ [0-9]{4})/i"; //This will match a letters + space + 4 digit number together
    $inputString = "juny 2014 october 2014 february 2016 march 2017 july 2010 FEBRUARY 2014";
    $matchCount = preg_match_all($regexPattern, $inputString, $matches);
    print_r($matches);
?>

你会得到这样的输出;

  

阵列

     

[0] => Array
    (
        [0] => juny 2014
        [1] => october 2014
        [2] => february 2016
        [3] => march 2017
        [4] => july 2010
        [5] => FEBRUARY 2014
    )


[1] => Array
    (
        [0] => juny 2014
        [1] => october 2014
        [2] => february 2016
        [3] => march 2017
        [4] => july 2010
        [5] => FEBRUARY 2014
    )
     

工作示例在这里http://ideone.com/LuQqEN

答案 1 :(得分:0)

我使用与cihan-uygun代码相同的代码,但是我的Regx表达式变化不大

这是代码

示例: 我需要这些字符串输出的日期

2020年冰港全年龄硕士/专家-2020年11月21日
2020年冰港学术公开赛(2020年11月21日-2020年11月22日)

<?php $regexPattern = "/[A-Z]{3} [0-9]{2}\, [0-9]{4}/i";                    
preg_match_all($regexPattern, $stringgosehere, $matches); ?>