how to convert timestamp to date in codeigniter

后端 未结 5 1086
暗喜
暗喜 2021-01-21 19:03

I want to convert 1373892900000 to Monday 2013/07/15 8:55 AM in Codeigniter.

However, I keep receiving a totally different result by converting

相关标签:
5条回答
  • 2021-01-21 19:44
    <?php
    $time_str=1373892900000;
    echo gmdate("fill with your format", $time_str);
    ?>
    

    your format = format your time in php, reading this page for details.

    http://php.net/manual/en/function.date.php
    http://php.net/manual/en/function.gmdate.php
    
    0 讨论(0)
  • 2021-01-21 19:48

    Why not just use PHP's date function?

    public function time_convert($timestamp){
       return date('l Y/m/d H:i', $timestamp);
    }
    

    For different timezones use a DateTime object:

    public function time_convert($timestamp, $timezone = 'UTC'){
        $datetime = new DateTime($timestamp, new DateTimeZone($timezone));
        return $datetime->format('l Y/m/d H:i');
    }
    

    Think that should work. Note: I tihnk you need at least PHP version 5.20 for the TimeZone class.

    0 讨论(0)
  • 2021-01-21 19:48

    Appears as though an invocation of standard_date with the DATE_ATOM format may sort you:

    echo unix_to_human(time(), true, 'us'); # returns 2013-07-12 08:01:02 AM, for example
    

    There are a whole host of other options for the format, enumerated on the linked page.

    0 讨论(0)
  • 2021-01-21 19:55

    This how to covert timestamp to date very simple:

    echo date('m/d/Y', 1299446702);
    

    to convert timestamp to human readable format try this:

    function unix_timestamp_to_human ($timestamp = "", $format = 'D d M Y - H:i:s')
    {
       if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
       return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
    }
    
    $unix_time = "1251208071";
    
    echo unix_timestamp_to_human($unix_time); //Return: Tue 25 Aug 2009 - 14:47:51
    

    if you want to convert it to a format like this: 2008-07-17T09:24:17Z than use this method

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

    for details about date: http://php.net/manual/en/function.date.php

    0 讨论(0)
  • 2021-01-21 20:00

    Your timestamp is coming from javascript on the client, I would guess, because it appears to be in milliseconds. php timestamps are in seconds. So to get the answer you want, first divide by 1000.

    Showing the full year would have made the issue more obvious, as you would have seen the year as 45,506.

    0 讨论(0)
提交回复
热议问题