Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将JSON中的时间转换为PHP实际播放的时间_Javascript_Php_Time - Fatal编程技术网

Javascript 将JSON中的时间转换为PHP实际播放的时间

Javascript 将JSON中的时间转换为PHP实际播放的时间,javascript,php,time,Javascript,Php,Time,我正在开发一个应用程序,我请求了整个游戏的播放时间,从他们购买到现在,对于一个特定的用户,这是我在JSON中得到的结果 { "TotalTimePlayed": "PT48M26.4570633S" } 我需要将其转换为:月、日、时、分、秒 在我看来,这就是我的变量的显示方式: {{ $TotalTimePlayed }} 我如何将其转化为可读的时间 /**********编辑****************/ 我从helpers文件中插入了prettyDate函数,但它显示了错误的时

我正在开发一个应用程序,我请求了整个游戏的播放时间,从他们购买到现在,对于一个特定的用户,这是我在JSON中得到的结果

{ 
  "TotalTimePlayed": "PT48M26.4570633S"
}
我需要将其转换为:月、日、时、分、秒

在我看来,这就是我的变量的显示方式:

{{ $TotalTimePlayed }}
我如何将其转化为可读的时间

/**********编辑****************/

我从helpers文件中插入了prettyDate函数,但它显示了错误的时间

{{ prettyDate($TotalTimePlayed) }}
在helper.php文件中:

function prettyDate($date) {
    return date("d h, I", strtotime($date));
}
/*****编辑******/

我希望它像这样,例如:
1米、22天、6小时、45米、56秒

持续时间格式

你可以这样做:

$playtime = $parts[0] . " months, " .
            $parts[1] . " days, " .
            $parts[2] . " hours, " .
            $parts[3] . " minutes, and " .
            $parts[4] . " seconds";
1.使用日期间隔 给定的格式几乎是PHP所期望的格式,只是它不允许小数

因此,我们可以首先删除该部分,然后使用该类生成输出:

$json = '{ 
  "TotalTimePlayed": "PT48M26.4570633S"
}';

// Interpret JSON 
$obj = json_decode($json);

// Get value, and strip fractional part (not supported by DateInterval)
$value = preg_replace("/\.\d+/", "", $obj->TotalTimePlayed);

// Turn this into a DateInterval instance
$interval = new DateInterval($value);

// Use format method to get the desired output
echo $interval->format('%m months, %d days, %h hours, %i minutes, %s seconds');
示例数据的输出为:

0个月0天0小时48分26秒

2.使用preg_match_all提取数字 此选项不使用
日期间隔
,因此可以处理小数秒:

// Sample data:
$json = '{ 
  "TotalTimePlayed": "PT48M26.4570633S"
}';

// Interpret JSON 
$obj = json_decode($json);

// Extract all numbers in that "PT" format into an array
preg_match_all("/[\d.]+/", $obj->TotalTimePlayed, $parts);

// Convert string representations to numbers
$parts = array_map('floatval', $parts[0]);

// Pad the array on the left in order to get 5 elements (months, days, hours, minutes, seconds)
$parts = array_pad($parts, -5, 0);

// Output (just for checking)
echo json_encode($parts);
产出:

[0,0,0,48,26.4570633]

如果不需要秒的小数部分,则将上述代码中的
'floatval'
替换为
'intval'

$parts = array_map('intval', $parts[0]);
然后,该示例将具有以下输出:

[0,0,0,48,26]

然后你可以这样做:

$playtime = $parts[0] . " months, " .
            $parts[1] . " days, " .
            $parts[2] . " hours, " .
            $parts[3] . " minutes, and " .
            $parts[4] . " seconds";

你希望人们玩一个单一的游戏。。。。几个月?至少尝试一下。任何东西…谷歌“JavaScript时间对象”你能创建一个我们可以使用的吗?这将使您更有可能获得高质量的问题答案。首先,请告诉我们,在数月、数天等时间内,这对应于什么。是48分钟还是数月?4570633是秒数吗?这与48和26有什么关系?好的,谢谢你的输入,我会看看我能做些什么