【问题标题】:json date object to php datejson日期对象到php日期
【发布时间】:2011-03-21 07:53:42
【问题描述】:

我正在调用一个 web 服务,它返回一个 json 对象。 json 对象对日期进行编码。我正在尝试找到一种方法将该日期转换为 php 中的 m-d-Y 格式。 Json 对象是 {"DateOfBirth":"/Date(387518400000-0400)/"} 这个日期是 02-15-1982。

我调用的 web 服务在 .NET 中,它将日期转换为 JSON 对象。不确定这是否会有所帮助。

提前致谢

谢谢, 天美

【问题讨论】:

  • 这是偶然的 04-12-1982,而不是 02-15-1982?
  • 使用 AinStain 的解决方案(如果日期为负数(1970 年之前),则该解决方案是必要的)该日期似乎是 1982-04-13。

标签: php .net javascript json date


【解决方案1】:

我知道这个问题/答案是旧的,但我想补充一点,@hookdonwinter 的答案不再正确。虽然他的回答可能已经解决了具体的解决方案,但在 2012 年,我们现在有一个额外的小数位需要处理。

echo parseJsonDate('/Date(1336197600000-0600)/', 'date');

public function parseJsonDate($date, $type = 'date') {
    preg_match( '/\/Date\((\d+)([+-]\d{4})\)/', $date, $matches); // Match the time stamp (microtime) and the timezone offset (may be + or -)

    $date = date( 'm-d-Y', $matches[1]/1000 ); // convert to seconds from microseconds

    switch($type)
    {    
        case 'date':
            return $date; // returns '05-04-2012'
            break;

        case 'array':
            return explode('-', $date); // return array('05', '04', '2012')
            break;

        case 'string':
            return $matches[1] . $matches[2]; // returns 1336197600000-0600
            break;
    }    
}   

【讨论】:

    【解决方案2】:

    @Bromomb

    您的函数运行良好,但您忘记了一件事。 对于 01.01.1970 之前的日期,时间戳可能为负数,因此我们需要不同的正则表达式

    我用过这个,效果很好:

    preg_match( '/\/Date\((-?\d+)([+-]\d{4})\)/', $date, $matches);
    

    最后,我稍微修改了您的功能,使其对我更有用。 现在我可以决定是否将日期返回为

    • "date" => 仅日期
    • "time" => 仅时间
    • "datetime" => 日期和时间
    • "array" => 日期和时间作为一个数组
    • "string" => 时间戳作为字符串

    当我给出第三个参数时,我可以决定是否要添加/减去与 UTC 时区的差异...

    function parseJsonDate($date, $type = 'date', $utc = 0) {
        // Match the time stamp (microtime) and the timezone offset (may be + or -) and also negative Timestamps
        preg_match( '/\/Date\((-?\d+)([+-]\d{4})\)/', $date, $matches);
    
        $seconds  = $matches[1]/1000;                // microseconds to seconds
        $UTCSec   = $matches[2]/100*60*60;           // utc timezone difference in seconds
    
        if($utc != 0){
            $seconds  = $seconds + $UTCSec; // add or divide the utc timezone difference
        }
    
        $date     = date( 'Y-m-d', $seconds );       // only date
        $dateTime = date( 'Y-m-d H:i:s', $seconds ); // date and time
        $time     = date( 'H:i:s', $seconds );       // only time
    
        switch($type)
        {
            case 'date':
                return $date; // returns 'YYYY-MM-DD'
                break;
    
            case 'datetime':
                return $dateTime; // returns 'YYYY-MM-DD HH:ii:ss'
                break;
    
            case 'time':
                return $time; // returns 'HH:ii:ss'
                break;
    
            case 'array':
                $dateArray = str_replace(" ", "-", $dateTime);
                $dateArray = str_replace(":", "-", $dateArray);
                return explode('-', $dateArray); // return array('YYYY', 'MM', 'DD', 'HH', 'ii', 'SS')
                break;
    
            case 'string':
                return $matches[1] . $matches[2]; // returns 1336197600000-0600
                break;
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果有可能您说的是 02-15-1982 但实际上是指 04-12-1982,那么我有一个解决方案。如果不是,那么在大约 60 天的时间里会有一个差距,这可以用更多的数学来解释。

      这是我现在的解决方案:

      date_default_timezone_set(  'America/Denver' );
      
      $json = json_decode( '{"DateOfBirth":"\/Date(387518400000-0400)\/"}' );
      $date_string = $json -> DateOfBirth;
      
      preg_match( '/([\d]{9})/', $date_string, $matches ); // gets just the first 9 digits in that string
      
      echo date( 'm-d-Y', $matches[0] );
      

      返回:04-12-1982

      【讨论】:

      • @hookedonwinter:感谢您的回复,但日期是 1982 年 2 月 15 日。今天对我来说,json 对象就像 - {"DateOfBirth":"\/Date(382597200000-0500)\/"} 再次这是 02/15/1982。我确实与 web 服务的 .NET 开发人员讨论过,他说就像 .NET 一样,它会为他自己解析日期并为他创建 JSON 对象。所以看起来.NET正在做一些特定的转换!!!让我添加 .NET TAG,如果有人可能知道的话。不过感谢您的帮助。
      • 在您的评论中,您的值为 382597200000,但在您的问题中,您的值为 387518400000。您说两者都是 1982 年 2 月 15 日,但我相信像 @hookedonwinter 一样,日期有错误问题中指定。如果是这种情况,hookedonwinter 的解决方案效果很好,所以我投票赞成。
      • @kevin:我相信作为一个时间戳,数字每天都会变化。我问的号码 387518400000-0400 是昨天的。如果我错了,请告诉我。 @hookdonwinter:您的逻辑确实对我有用,除非我确实设置了时区,就像您在代码中所做的那样,它给了我 04-12-192,但是如果我没有指定时区,它给了我 02-15 -1982 年。目前我在 EST 时区。我在约会时间方面没有太多经验。但你的建议确实对我有用。非常感谢。标记为正确答案。
      • 嗯...这很奇怪。但是,嘿,它有效 ::applies band-aid::
      • @jtanmay - 整个事情看起来不像一个时间戳。第一部分的前 9 位数字是,这就是 hookedonwinter 的解决方案将它们拉出的原因。我冒昧地猜测“-0500”部分是时区偏移量,但我不知道其他 3 位数字是干什么用的。关于时区,从 php 5.1.0 开始,如果您要使用 php 函数(请参阅php.net/manual/en/function.date-default-timezone-set.php),您应该始终设置默认设置,因此您可能需要考虑在某个时候进一步调查。
      【解决方案4】:

      你可以使用这个包来解析 JSON 日期:

      https://github.com/webapix/dot-net-json-date-formatter

      use \Webapix\DotNetJsonDate\Date;
      
      Date::toDateTime('/Date(387518400000-0400)/'); // return with \DateTime object
      

      【讨论】:

        【解决方案5】:

        @Brombomb 在我的情况下,我向您的函数添加了另一个参数以返回日期对象:

        public function parseJsonDate( $date, $type = 'date' )
        {
            // removes extra millisecond digits in an other reg exp class
            preg_match( '/\/Date\((\d{10})\d+([+-]\d{4})\)/', $date, $matches ); // Match the time stamp (microtime) and the timezone offset (may be + or -)
        
            $date = date( 'm-d-Y', $matches[1] );
        
            switch( $type )
            {
                case 'dateTimezone':
                    return DateTime::createFromFormat( 'UT', $matches[1] . $matches[2] );
        
                case 'date':
                    return $date; // returns '05-04-2012'
                    break;
        
                case 'array':
                    return explode( '-', $date ); // return array('05', '04', '2012')
                    break;
        
                case 'string':
                    return $matches[1] . $matches[2]; // returns 1336197600000-0600
                    break;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-06
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 2012-06-07
          • 2017-04-25
          • 1970-01-01
          • 2011-02-17
          相关资源
          最近更新 更多