dongxili9934 2017-03-07 18:57
浏览 17
已采纳

根据日期显示月份

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.

  • 写回答

1条回答 默认 最新

  • dongxian6715 2017-03-07 19:05
    关注

    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];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料