首页 文章

将UNIX时间戳转换为格式化日期字符串

提问于
浏览
151

使用PHP,我想将UNIX时间戳转换为类似于此的日期字符串: 2008-07-17T09:24:17Z

如何将时间戳如 1333699439 转换为 2008-07-17T09:24:17Z

8 回答

  • 103

    设置默认时区以获得正确的结果非常重要

    <?php
    // set default timezone
    date_default_timezone_set('Europe/Berlin');
    
    // timestamp
    $timestamp = 1307595105;
    
    // output
    echo date('d M Y H:i:s Z',$timestamp);
    echo date('c',$timestamp);
    ?>
    

    在线转换帮助:http://freeonlinetools24.com/timestamp

  • 13
    $unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);
    

    这应该工作 .

  • 45
    <?php
    $timestamp=1486830234542;
    echo date('Y-m-d H:i:s', $timestamp/1000);
    ?>
    
  • 0

    试试这样的gmdate

    <?php
    $timestamp=1333699439;
    echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
    ?>
    
  • 6

    使用日期功能 date ( string $format [, int $timestamp = time() ] )

    使用 date('c',time()) 作为格式转换为ISO 8601日期(在PHP 5中添加) - 2012-04-06T12:45:47+05:30

    使用 date("Y-m-d\TH:i:s\Z",1333699439) 获取 2012-04-06T13:33:59Z

    以下是日期功能支持的一些格式

    <?php
    $today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
    $today = date("m.d.y");                         // 03.10.01
    $today = date("j, n, Y");                       // 10, 3, 2001
    $today = date("Ymd");                           // 20010310
    $today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
    $today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
    $today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
    $today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
    $today = date("H:i:s");                         // 17:16:18
    ?>
    
  • 268

    假设您使用的是PHP5.3,那么处理日期的现代方式是通过原生的DateTime class . 要获得当前时间,您可以致电

    $currentTime = new DateTime();
    

    从特定时间戳创建DateTime对象(即不是现在)

    $currentTime = DateTime::createFromFormat( 'U', $timestamp );
    

    要获得格式化的字符串,您可以调用

    $formattedString = $currentTime->format( 'c' );
    

    manual page here

  • 5

    我发现这个对话中的信息非常有用,我只想通过使用MySQL数据库中的时间戳和一点PHP来添加我的想法 .

    <?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>
    

    输出结果为:2017-03-03T08:22:36 01:00

    非常感谢Stewe你的回答对我来说是一个尤里卡 .

  • -3

    检查这个网址http://www.epochconverter.com/ .

    拥有所有语言Epoch和Unix时间戳 .

相关问题