Reputation:
I have been writing code to show a month, according to the day of the current month.
For example:
If we are between the 1st and the 10th of the actual month, I want to display the actual month. If we are after the 10th of the actual month, I want to display the next month.
So, if we are the March, 1st, I want 'March' to display.
But if we were the March, 22, I want 'April' to display.
I wanted the months displaying in Spanish. I've already done this by the following code.
Code:
function date_es($format = 'F', $time = null){
if(empty($time)) $time = time();
$date = date($format, $time);
$mois_en = array("January","February","March","April","May","June","July","August","September","October","November","December");
$mois_es = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
$date = str_replace($mois_en, $mois_es, $date);
return $date;
}
However, I do not know how to include the conditional statements to this code ?
Cheers for any help.
Upvotes: 0
Views: 50
Reputation: 54831
If you need to get day of the month - use j
formating option.
In my opinion you function should look like:
function date_es($format = 'F', $time = null){
if(empty($time)) $time = time();
// get day num
$day_num = date('j', $time);
// get month num
$month_num = date('n', $time);
if ($day_num > 10) {
// add 1 if day is more then 10
$month_num += 1;
// if your month is December,
// then `$month_num` is 13
// but you don't need this)
$month_num %= 12;
}
$mois_es = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
// return month name
//I added `- 1` because keys in `$mois_es` start with zero
return $mois_es[$month_num == 0? 11 : $month_num - 1];
}
Upvotes: 1