Reputation:
I am tring to show a month name dependant on a numeric value. So if 11, show November, if 9 show September and so on.
Here is my code:
$month=$_GET["q"];
if($month['month'] == '11')
{
echo"November";
}
if($month['month'] == '10')
{
echo"October";
}
if($month['month'] == '9')
{
echo"September";
}
This doesn't show anything? Can anyone help?
Upvotes: 0
Views: 235
Reputation: 4696
You can simply use string to time.
$month=$_GET["q"];
echo date('F', strtotime('2012-' . $month . '-01'));
This will give you the full name of the month dependent of what number the month variable is assigned.
Strtotime requires the date, hence why 2012 etc is in there also, albeit irrelevant to the output.
Upvotes: -1
Reputation: 1063
If $_GET['q']
has your month number, you need to use only $month
and not $month['month']
.
Probably, this is best way to achieve your result using Non-OOP PHP:
$month = $_GET['q'];
$monthName = date("F", mktime(0, 0, 0, $month));
echo $monthName;
Upvotes: 2
Reputation: 1477
because $_GET["q"] can't be an array, so you can't use $month['month'], use $month directly
and instead of so many if else you can use, the following code to echo month name directly.
$monthName = date("F", mktime(0, 0, 0, $month));
echo $monthName;
More details at http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.mktime.php
Upvotes: 0
Reputation: 1223
here in your case $month is variable not a array so try to use like this...
if($month=='11')
echo 'Nov';
Upvotes: 0
Reputation: 28753
It will like this
if($month == '11') {
}
Because you are directly assigning the GET
value to $month
.Assuemed that q
will be the month name.
Upvotes: 0
Reputation: 286
$month = date('n');
if($month == '11')
{
echo "November";
}
if($month == '10')
{
echo "November";
}
make sure you get the correct value eg:10,11... for month variable
Upvotes: 0
Reputation: 76636
There's no need to create multiple if
statements. You can simply use DateTime::createFromFormat
to achieve this:
$month_number = (int) $_GET["q"];
if($month_number > 0 && $month_number <= 12)
echo DateTime::createFromFormat('m', $month_number)->format('F');
Upvotes: 2
Reputation: 349
Try This:
$monthArr = array('1'=>'Jan','2'=>'Feb');
echo $monthArr[$month];
Upvotes: 0