代码之家  ›  专栏  ›  技术社区  ›  soniccool

将日期格式化为可读格式

  •  33
  • soniccool  · 技术社区  · 12 年前

    比方说,在我的mysql数据库中,我有一个时间戳 2013-09-30 01:16:06 假设这个变量是 $ts .

    我如何输出此内容以显示更多类似内容 September 30th, 2013 ?

    7 回复  |  直到 9 年前
        1
  •  63
  •   Serhii Kheilyk    4 年前
    $timestamp = "2013-09-30 01:16:06";
    echo date("F jS, Y", strtotime($timestamp)); //September 30th, 2013
    

    注意使用 S 获取当天的英语序数后缀。
    由于您已经在使用 strtotime 如果您需要人类可读的数据 当前时间 你可以使用关键字“ 现在 “如中所示 strtotime("now")

    相关来源

        2
  •  25
  •   Hanky Panky    12 年前

    使用 strtotime() 要将该字符串转换为Unix时间戳,请使用 date() 函数可以随心所欲地显示。

    echo date("F j, Y, g:i a",strtotime($ts)); 
    

    参考文献:

    http://www.php.net/manual/en/function.strtotime.php

    http://www.php.net/manual/en/function.date.php

        3
  •  4
  •   Hanky Panky    12 年前

    我认为那里甚至不需要php。检查MySQL DATE_FORMAT() 作用

    例子:

    SELECT DATE_FORMAT('2013-09-30 01:16:06', '%M %D, %Y %H:%i:%s') as `readable`;
    

    结果:

    September 30th, 2013 01:16:06
    

    对于实际使用:

    SELECT
        DATE_FORMAT(`date_column`, '%M %D, %Y %H:%i:%s') as `readable`
    FROM
        `your_table`;
    
        4
  •  3
  •   Phd. Burak Öztürk    12 年前
    echo date("F d Y",strtotime("2013-09-30 01:16:06"))
    
        5
  •  1
  •   Shudmeyer    12 年前

    类似于:

    <?php
    $date = '2013-09-30 01:16:06';
    $convertDate = date('F jS, Y h:i:s', strtotime($date));
    echo $convertDate;
    ?>
    
        6
  •  1
  •   Anup Singh    12 年前

    Mysql查询

     select UNIX_TIMESTAMP(datetime_field) as datetime_field from table;
    

    PHP程序

    echo date("F d Y", $datetime_field )
    
        7
  •  -1
  •   Jack Bashford    6 年前

    使用 diffForHumans 功能:

    Carbon\Carbon::parse($ts)->diffForHumans()