Parse multiple dates from same string in php -


i had technical test , 1 of things had program has input of 2 dates, , has output months 5 sundays between these 2 dates. string $dates has 2 dates in same line example: juny 2014 october 2014 need use these 2 dates stuff.

in point, thinking 2 separate strings work each date, this:

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

then:

$firstdate = $dates[0].$dates[1]; $seconddate = $dates[2].$dates[3]; 

but see way of doing things obvious , not dynamyc. knows better or more elegant/apropriate way this?

edit: full code

<?php date_default_timezone_set('utc');  //output month 5 sundays  foreach(file($argv[1]) $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 month first date         $date[0] = '01-'.$date[0].'-'.$date[1];         //get lastmonthday         $lastmonthday = date('t', strtotime($date[2].'-'.$date[3]));          //add last day of month 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 earlier 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"); } ?> 

you can create simple regex operation can check out code below;

<?php     header('content-type: text/plain; charset=utf8');     $regexpattern = "/([a-z]+ [0-9]{4})/i"; //this match letters + space + 4 digit number     $inputstring = "juny 2014 october 2014 february 2016 march 2017 july 2010 february 2014";     $matchcount = preg_match_all($regexpattern, $inputstring, $matches);     print_r($matches); ?> 

you output that;

array

(

[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     ) 

)

and working example here http://ideone.com/luqqen


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -