duanlei2458 2017-06-13 13:50
浏览 35

PHP,日期:附加到月份简短表示的点

I have

date("M.", $datetime)

I want to get this output:
Jan.
Feb.
Mar.
Apr.
May (no dot necessary)
Jun.
Jul.

I dont like the idea of an if-statement to check the length/number of month every time a date is generated.

Is there a approach that is more simple? Like changing the month-name in general? Or hooking into the date function itself to implement an if-statement that runs every time the date function runs.

Thanks

  • 写回答

2条回答 默认 最新

  • douqiu1604 2017-06-13 13:52
    关注

    If you don't want the dot to appear after May month, you will need a check of some sort - which normally is an if. You could do something like this, check if the month returned by date() isn't May, and add a dot after if it isn't.

    $date = date("M", $datetime);
    if (date("M") != "May")
        $date .= ".";
    

    Otherwise you'd need to implement a function of your own, but in the end - you will have to end up with this again, there's really no way around it - and this is by far the simplest and cleanest way.

    You could wrap this into a function. You can't alter the date() function directly, but you can create one of your own.

    function my_date($format, int $timestamp = null) {
        if ($timestamp === null)
            $timestamp = time();
    
        $date = date($format, $timestamp);
        if ($format == "M" && date("M", $timestamp) != "May")
            $date .= ".";
        return $date;
    }
    

    Then use it as

    echo my_date("M", $datetime);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图