0

Not as simple as it sounds I believe, but I have the following and trying to print the month name from date provided.

public function convertEngDateToAr($engDate)
    {
        //SELECT MONTHNAME('2009-05-18');
        $connectionInstance = new ConnectionClass();
        $connection = $connectionInstance->connectToDatabase();
        $monthResult = $connection->query("SELECT MONTHNAME('$engDate')");
        //echo "Month: " . $monthResult;
        while($row = $monthResult->fetch_assoc())
        {
            print_r($row);
        }

    }

The above gets me this output:

Array ( [MONTHNAME('2012-08-21')] => August )

How can I get August alone out of the array and store it in variable? The only weird thing here is that the index in the array is just strange.

Thank

2 Answers 2

3

Try changing SELECT MONTHNAME('$engDate')" to SELECT MONTHNAME('$engDate') as monthName"

Then you should see:

Array ( monthName => August )

Then you can use:

    while($row = $monthResult->fetch_assoc())
    {
        $myMonth=$row['monthName'];
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You can either use SELECT MONTHNAME(xxxx) AS monthName or you could return an indexed array instead of an associative one:

while($row = $monthResult->fetch_row())
  echo $row[0] . "\n";

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.