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.
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.
Comments