1

I am new to php and am trying to write a script that takes a time stamps in the form of "January 23, 2014 at 11:01PM" and breaks them into an array of month, date, year, hour, and minute. Here is my code so far:

        $raw_data= array("January 20, 1993 at 10:20PM", "September 6, 1991 at 6:23PM");
        var_dump($raw_data);
        $num_dates= count($raw_data);

//Step 1: break content into month, day, year, hour, and minutes
    for ($i=0; $i==($num_dates-1); $i++) {  
        $partial_data = implode(preg_split("/[A-Z]{2}/", $raw_data[$i])); 
        $broken_data[$i] = preg_split("/[\s,:]/", $partial_data);        
        unset($broken_data[$i][2]);
        unset($broken_data[$i][3]);
    }

    var_dump($broken_data);

The code seems to run fine without the var_dump, but when I add it php tells me that $broken_data is an undefined variable. Anyone see an obvious rookie mistake I'm making?

Thanks in advance.

2
  • 2
    Did obtain any knowledge from your previous question? I do not think so stackoverflow.com/questions/26308756/formatting-php-for-loop/… Commented Oct 11, 2014 at 0:37
  • you have unset all the elements of array maybe. So at the end the array is totally unset. Commented Oct 11, 2014 at 0:47

1 Answer 1

1

Change your loop condition to use comparison, not equality.

for ($i = 0; $i < ($num_dates - 1); $i++)

The code inside your loop is never executed because the loop condition fails on the first try.

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

1 Comment

Oooo! I learned to code in MatLab where the for loop is denoted from i=__ to i=__. I thought I was telling the loop to go from zero until (num_dates-1). Thanks!

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.