V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jacob
V2EX  ›  问与答

php 中的 strftime 能不能返回中文日期

  •  
  •   jacob · 2015-03-31 17:17:55 +08:00 · 2752 次点击
    这是一个创建于 3549 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是不是要和setlocale一起用,

    我想要实现这样的功能,有没有高级点的写法:

    function chinese_date($timestamp)
    {
    
        $months=array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
        $m = (int) date('n',strtotime($timestamp));
        $j = date('j',strtotime($timestamp));
    
        $c_date= $months[$m-1].$j;
    
    
        if (substr($timestamp,0,4) == date('Y')) {
            return $c_date;
        } else {
            return date('Y',strtotime($timestamp)).'年'.$c_date;
        }
    }
    
    11 条回复    2015-05-13 12:48:41 +08:00
    touch
        1
    touch  
       2015-03-31 18:04:47 +08:00   ❤️ 1
    高级写法不知道
    简单点的写法
    $ar = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
    $time = date('Y年n月d日',time());
    preg_match('|年(.*)月|',$time,$mat);
    echo preg_replace('|.月|', $ar[$mat[1]-1],$time);
    haiyang416
        2
    haiyang416  
       2015-03-31 18:11:16 +08:00
    这函数格式出来的有什么特别的用处吗?
    2015年三月31,2015年三月三十一,怎么看怎么别扭。

    二零一五年三月三十一日,如果是这个格式倒还能理解。
    anewg
        3
    anewg  
       2015-03-31 18:16:24 +08:00   ❤️ 1
    ```
    function chinese_date($timestamp, $output_delimiter = ' '){
    $info_arr = explode(' ',date('Y m d H i s',strtotime('now')));

    return implode($output_delimiter, array_map(function($v,$k){
    switch($k){
    case '0':
    // 处理年
    return $v.'年';
    case '1':
    // 处理月
    return $v;
    // ......
    default:
    // 不做处理的
    return $v;
    break;
    }
    }, $info_arr, array_keys($info_arr)));
    }
    echo chinese_date('now','*'); // 2015年*03*31*12*11*19
    ```
    cevincheung
        4
    cevincheung  
       2015-03-31 18:30:41 +08:00
    http://cn2.php.net/manual/zh/book.datetime.php

    $date = new DateTime('now');
    echo strtr($date->format('n'),array_combine(range(1,12),array('一','二','三','四','五','六','七','八','九','十','十一','十二'))).'月';
    cevincheung
        5
    cevincheung  
       2015-03-31 18:37:55 +08:00   ❤️ 1
    function chinese_date($timestamp) {

    $date = new DateTime($timestamp);
    $months = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二');

    $m = $date->format('n');
    $j = $date->format('j');

    $c_date = $months[$m - 1].'月' . $j;

    if ($date->format('Y') == date('Y')) {
    return $c_date;
    } else {
    return $date->format('Y') . '年' . $c_date;
    }
    }

    不知道楼主想干啥
    cevincheung
        6
    cevincheung  
       2015-03-31 18:47:21 +08:00
    function chinese_date($timestamp) {

    $date = new DateTime($timestamp);
    $months = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二');

    $m = $date->format('n');
    $j = $date->format('j');

    $c_date = $months[$m - 1].'月' . $j;

    if ($date->format('Y') == date('Y')) {
    return $c_date;
    } else {
    return $date->format('Y') . '年' . $c_date;
    }
    }

    不知道楼主想干啥
    jacob
        7
    jacob  
    OP
       2015-03-31 19:07:30 +08:00
    @cevincheung 基本属于胡搞- -
    lianyue
        8
    lianyue  
       2015-03-31 20:25:40 +08:00   ❤️ 1
    https://github.com/lian-yue/loli.base/blob/master/src/Loli/Date.php


    看看吧 取消 掉
    self::$allTimezone = array_merge(DateTimeZone::listIdentifiers(), self::$allTimezone);
    的注视然后
    lang 方法是本地化语言翻译
    lang方法文件数组


    return [

    // 时区输出
    '-12:00' => 'UTC-12:00',
    '-11:30' => 'UTC-11:30',
    '-11:00' => 'UTC-11:00',
    '-10:30' => 'UTC-10:30',
    '-10:00' => 'UTC-10:00',
    '-09:30' => 'UTC-09:30',
    '-09:00' => 'UTC-09:00',
    '-08:30' => 'UTC-08:30',
    '-08:00' => 'UTC-08:00',
    '-07:30' => 'UTC-07:30',
    '-07:00' => 'UTC-07:00',
    '-06:30' => 'UTC-06:30',
    '-06:00' => 'UTC-06:00',
    '-05:30' => 'UTC-05:30',
    '-05:00' => 'UTC-05:00',
    '-04:30' => 'UTC-04:30',
    '-04:00' => 'UTC-04:00',
    '-03:30' => 'UTC-03:30',
    '-03:00' => 'UTC-03:00',
    '-02:30' => 'UTC-02:30',
    '-02:00' => 'UTC-02:00',
    '-01:30' => 'UTC-01:30',
    '-01:00' => 'UTC-01:00',
    '-00:30' => 'UTC-00:30',
    '+00:00' => 'UTC+00:00',
    '+00:30' => 'UTC+00:30',
    '+01:00' => 'UTC+01:00',
    '+01:30' => 'UTC+01:30',
    '+02:00' => 'UTC+02:00',
    '+02:30' => 'UTC+02:30',
    '+03:00' => 'UTC+03:00',
    '+03:30' => 'UTC+03:30',
    '+04:00' => 'UTC+04:00',
    '+04:30' => 'UTC+04:30',
    '+05:00' => 'UTC+05:00',
    '+05:30' => 'UTC+05:30',
    '+05:45' => 'UTC+05:45',
    '+06:00' => 'UTC+06:00',
    '+06:30' => 'UTC+06:30',
    '+07:00' => 'UTC+07:00',
    '+07:30' => 'UTC+07:30',
    '+08:00' => 'UTC+08:00',
    '+08:30' => 'UTC+08:30',
    '+08:45' => 'UTC+08:45',
    '+09:00' => 'UTC+09:00',
    '+09:30' => 'UTC+09:30',
    '+10:00' => 'UTC+10:00',
    '+10:30' => 'UTC+10:30',
    '+11:00' => 'UTC+11:00',
    '+11:30' => 'UTC+11:30',
    '+12:00' => 'UTC+12:00',
    '+12:30' => 'UTC+12:30',
    '+12:45' => 'UTC+12:45',
    '+13:00' => 'UTC+13:00',
    '+13:30' => 'UTC+13:30',
    '+13:45' => 'UTC+13:45',
    '+14:00' => 'UTC+14:00',



    // D 缩写本地化
    'D_Sun' => '日',
    'D_Mon' => '一',
    'D_Tue' => '二',
    'D_Wed' => '三',
    'D_Thu' => '四',
    'D_Fri' => '五',
    'D_Sat' => '六',


    'l_Sunday' => '星期日',
    'l_Monday' => '星期一',
    'l_Tuesday' => '星期二',
    'l_Wednesday' => '星期三',
    'l_Thursday' => '星期四',
    'l_Friday' => '星期五',
    'l_Saturday' => '星期六',



    'F_January' => '一月',
    'F_February' => '二月',
    'F_March' => '三月',
    'F_April' => '四月',
    'F_May' => '五月',
    'F_June' => '六月',
    'F_July' => '七月',
    'F_August' => '八月',
    'F_September' => '九月',
    'F_October' => '十月',
    'F_November' => '十一月',
    'F_December' => '十二月',





    'M_Jan' => '一',
    'M_Feb' => '二',
    'M_Mar' => '三',
    'M_Apr' => '四',
    'M_May' => '五',
    'M_Jun' => '六',
    'M_Jul' => '七',
    'M_Aug' => '八',
    'M_Sep' => '九',
    'M_Oct' => '十',
    'M_Nov' => '十一',
    'M_Dec' => '十二',




    'a_am' => '上午',
    'a_pm' => '下午',

    'A_AM' => '上午',
    'A_PM' => '下午',
    'S_st' => '下午',


    '$1 second' => '$1 秒',
    '$1 seconds' => '$1 秒',

    '$1 min' => '$1 分钟',
    '$1 mins' => '$1 分钟',

    '$1 hour' => '$1 小时',
    '$1 hours' => '$1 小时',

    '$1 day' => '$1 天',
    '$1 days' => '$1 天',

    '$1 month' => '$1 月',
    '$1 months' => '$1 月',

    '$1 year' => '$1 年',
    '$1 years' => '$1 年',

    '$1 ago' => '$1前',
    '$1 later' => '$1后',

    'Now' => '刚刚',
    ];
    lianyue
        9
    lianyue  
       2015-03-31 20:26:08 +08:00
    我以前自己写的本地化语言包
    lianyue
        10
    lianyue  
       2015-03-31 20:27:56 +08:00
    然后注视有的地方有误哦 可能请忽略注视看
    jacob
        11
    jacob  
    OP
       2015-05-13 12:48:41 +08:00
    @lianyue 传说的恋月,么么哒
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3508 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 04:41 · PVG 12:41 · LAX 20:41 · JFK 23:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.