How to print random months in PHP

Rinku

Is a function available in PHP, which can print random month?

Like mt_rand(); to print random number.

If NO, what will easy method to print random month?

I am using below code:

$randmo = mt_rand(1,12);
if($randmo == 1)
{
    $randmonth = "Jan";
}
elseif($randmo == 2)
{
    $randmonth = "Feb";
}
elseif($randmo == 3)
{
    $randmonth = "Mar";
}
elseif($randmo == 4)
{
    $randmonth = "Apr";
}
elseif($randmo == 5)
{
    $randmonth = "May";
}
elseif($randmo == 6)
{
    $randmonth = "Jun";
}
elseif($randmo == 7)
{
    $randmonth = "Jul";
}
elseif($randmo == 8)
{
    $randmonth = "Aug";
}
elseif($randmo == 9)
{
    $randmonth = "Sep";
}
elseif($randmo == 10)
{
    $randmonth = "Oct";
}
elseif($randmo == 11)
{
    $randmonth = "Nov";
}
else
{
    $randmonth = "Dec";
}
echo $randmonth;

First i am storing a rand number from 1 to 12 in $randmo variable. And printing using if else. I am looking for better script.

Neel Ion

You can do it like this. array_rand() function returns a random key of an array.

<?php

$months=array('january','february','march','april','may','june','july','august','september','october','november','december');

$key=array_rand($months);

echo $months[$key];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to decrease months in php?

From Dev

how to decrease months in php?

From Dev

php mysql print months and table content

From Dev

How to print or echo a random php string on page load or refresh?

From Dev

how to print random cout statement?

From Dev

How can I generate a random number every x amount of months?

From Dev

How do I loop over the months of the year and print them in python?

From Dev

How to list the last N months with PHP DateTime

From Dev

How to get absolute number of months ago in PHP

From Dev

How do I compare months in PHP?

From Dev

How to list the last N months with PHP DateTime

From Dev

How to random the choices in php

From Dev

Print months between -6 and +6 months

From Dev

How to print a random character from a string?

From Dev

How to Print Random number in a Math Table?

From Dev

How to print the random.randrange number that was selected?

From Dev

How would I print a random array element?

From Dev

How to Print Random Words from a Text File

From Dev

How to properly use sleep with print and random in python

From Dev

How to print @<something> in php?

From Dev

How to print @<something> in php?

From Dev

How to print JavaScript with PHP

From Dev

How random is PHP pseudo random, of 4 bytes

From Dev

How to call random Function in PHP

From Dev

How to random the order of a list in PHP

From Dev

How to echo random text in PHP?

From Dev

How To Display Random Results in PHP

From Dev

How to check date range more than 6 months in php?

From Dev

How to calculate 2 months from today using php date