0

I had a technical test and one of the things I had to do is a program that has an input of two dates, and has to output the months with 5 sundays between these two dates. A string $dates has the two dates in the same line example: juny 2014 october 2014 I need to use these two dates to do stuff.

In this point, I was thinking to do two separate strings to work with each date, like this:

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

Then:

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

But I see this way of doing things too obvious and not dynamyc. Somebody knows a better or more elegant/apropriate way to do this?

Edit: this is the full code

<?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");
}
?>
4
  • 2
    they are basically trying to see your knowledge of iterations along with manipulating the date with PHP. What have you tried in order to get the 5 Sundays you mentioned? Commented Jan 27, 2016 at 18:23
  • I did it with a for loop. First I get the total days of the months, then I get the first day of the month and with a for loop I go through first to last day of the month calculating how many iterations are sunday. What do you think it would be the better approach? I can edit up there if you need to see more code.. Commented Jan 27, 2016 at 18:40
  • please edit your original question (you can do that by clicking the "edit" link above) and paste the code you mentioned to get the 5 Sundays. Commented Jan 27, 2016 at 18:42
  • Sure, I've added all the code. Sorry for the delay, was very busy! Commented Jan 29, 2016 at 9:02

2 Answers 2

1

You can create a simple regex for this operation can you check out the code below;

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

You will get an output like 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 is here http://ideone.com/LuQqEN

Sign up to request clarification or add additional context in comments.

Comments

0

i use same as cihan-uygun code but my Regx expression is little change

here is code

example : I need dates output from these strings

2020 Ice Harbor All-Ages Master/Expert - Nov 21, 2020
2020 Ice Harbor Scholastic Open (Nov 21, 2020 - Nov 22, 2020)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.