0

so i have an array with months:

$arr_month = array(
    1=>'january',
    2=>'february',
    3=>'march',
    4=>'april',
    5=>'may',
    6=>'june',
    7=>'july',
    8=>'august',
    9=>'september',
    10=>'october',
    11=>'november',
    12=>'december'
);

i also have strings (i know, they look weird...) that look like:

23 de january del 2003
12 de may de 1976
6 de february de 1987

What i want is to find and match the month in the string with the array and return the array key.

so:

23 de january del 2003 returns 1
12 de may de 1976 returns 5
6 de february de 1987 returns 2

and so on...

Any idea how?

5 Answers 5

1

This should work for you.

$dateString = "23 de january del 2003"; // as an example
$exploded = explode (" ", $dateString); // separate the string into an array of words
$month = $exploded[2]; // we want the 3rd word in the array;
$key = array_search ($month, $arr_month); // lets find the needle in the haystack
print $key;

yields 1.

See http://php.net/manual/en/function.array-search.php for more.

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

Comments

1
$index = -1;
foreach ($arr_month as $k => $v) {
   if (strstr($mystring, $v)) {
      $index = $k;
      break;
   }
}
// exists with $index set

Comments

1

You should be able to do like this:

<?php

function getMonth($time){
    $matches = array();
    preg_match("/[0-9] de ([a-z]+) de/", $time, $matches);
    return date('n',strtotime($matches[1]));
}
?>

Then you don't even need your month array :D

EDIT if you for some reason want the array in there:

<?php
function getMonth($time){
    $matches = array();
    $pattern = "/[0-9] de ([a-z]+) de/";
    preg_match($pattern, $time, $matches);
    $month = $matches[1];
    $arr_month (
        1=>'january',
        2=>'february',
        3=>'march',
        4=>'april',
        5=>'may',
        6=>'june',
        7=>'july',
        8=>'august',
        9=>'september',
        10=>'october',
        11=>'november',
        12=>'december'
    );
    return in_array($month, $arr_month) ? array_search($month, $arr_month) : false;
}
?>

Comments

1

You can do this with the optional search param with array keys:

$matchedKeys = array_keys($arr_month, "november");
var_dump($matchedKeys);
// array(1) { [0]=> int(11) }

As you can see this will return an array of all matching keys.

Comments

1

If you're trying to parse a date, bear in mind that PHP can do that for you (though it depends on how arbitrary your input data is - this is obviously more reliable if you know the date format in advance).

eg:

print_r(date_parse("6 de february de 1987"));

gives:

Array
(
    [year] => 1987
    [month] => 2
    [day] => 
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 2
    [warnings] => Array
        (
            [14] => Double timezone specification
            [22] => The parsed date was invalid
        )

    [error_count] => 2
    [errors] => Array
        (
            [0] => Unexpected character
            [2] => The timezone could not be found in the database
        )

    [is_localtime] => 1
    [zone_type] => 0
)

So it's given up on the day, but it did correctly identify the month and year.

Using the more modern DateTime api fails on your input (is that a mixture of French and English?)

But it does work for the following:

$d = new DateTime("6th february 1987", new DateTimeZone("UTC"));
print $d->format("Y-m-d H:i:s") . "\n";'

Gives:

1987-02-06 00:00:00

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.