简体   繁体   English

在PHP中将ISO 8601时间戳转换为多维数组中的Unix时间戳

[英]Convert iso 8601 timestamps to unix timestamp in multidimensional array in PHP

I have a script that receives all kinds of different data arrays from a web based API. 我有一个脚本,可以从基于Web的API接收各种不同的数据数组。 That program returns all dates in the ISO 8601 format (2013-08-12T09:00:00:000-0600). 该程序以ISO 8601格式(2013-08-12T09:00:00:000-0600)返回所有日期。 I have a function that will convert those easily to a unix timestamp, but I have to submit the value field to that function each time which can become tedious. 我有一个可以轻松将其转换为unix时间戳的函数,但是每次必须将值字段提交给该函数,这可能会变得很乏味。

The trick is, depending on what operation I am running at the time, the response array from the web app will be very different. 诀窍在于,根据我当时正在执行的操作,Web应用程序的响应数组将有很大不同。 Single dimensions and multidimensional. 单一维度和多维维度。 There are also numerous key names for the date holders. 日期持有者也有许多键名。

So, is there some way to easily submit a full array of data to a function that will seek out the ISO timestamps and convert them to a unix timestamp in-place, returning the same array with the swapped out timestamps? 因此,是否有某种方法可以轻松地向函数提供完整的数据数组,该函数将查找ISO时间戳并将其转换为就地的unix时间戳,并返回与交换出的时间戳相同的数组?

Here is the function I use to convert them one at a time: 这是我用来一次将它们转换一次的功能:

function tstamptotime($tstamp)
{
    // converts ISODATE to unix date
    // 1984-09-01T14:21:31Z
    sscanf($tstamp, "%u-%u-%uT%u:%u:%uZ", $year, $month, $day, $hour, $min, $sec);
    $newtstamp = mktime($hour, $min, $sec, $month, $day, $year);
    return $newtstamp;
}

Here is what I had come up with, but I realized that this idea would not take into account multidimensional arrays, and the trick is that the array could be anywhere from 2-25 levels deep: 这是我想出的,但是我意识到这个想法不会考虑多维数组,而诀窍在于数组的深度可能在2到25层之间:

foreach($results['data'] as $key => $value) {
            if(strpos($key,'Date') !== false) {
                if(strlen($value) == 28) {
                    $results['data'][$key] = tstamptotime($value);
                }
            }
        }

As you can see, the key of the value will always contain the word "Date" as part of the key name. 如您所见,值的键将始终包含单词“ Date”作为键名的一部分。

If it helps, the "results['data']" array in my example comes from a json string where I use json_encode to create the array. 如果有帮助,我的示例中的“ results ['data']”数组来自一个json字符串,我在其中使用json_encode创建该数组。 So I do have all values in plain text as well if something can be done there. 因此,如果可以在其中进行处理,那么我的所有值也都以纯文本格式显示。

Thanks! 谢谢!

$res = $results['data'];
array_walk_recursive($res, function (&$e, $k){
    if(strpos($k, 'Date') !== false) {
        if(strlen($e) == 28) {
            sscanf($e, "%u-%u-%uT%u:%u:%uZ", $year, $month, $day, $hour, $min, $sec);
            $e = mktime($hour, $min, $sec, $month, $day, $year);
        }
    }
});
$results['data'] = $res;

Since I don't know what your data looks like, I used the following to test: 由于我不知道您的数据是什么样子,因此我使用以下代码进行测试:

$results['info'] = array('some','info','here');
$results['data'][] = array('Date1' => '2013-08-09T09:00:00:000-0600');
$results['data'][] = array('Date2' => '2013-08-10T09:30:00:000-0600');
$results['data'][] = array('Date3' => '2013-08-11T09:45:00:000-0600');
$results['data'][] = array('Date4' => '2013-08-12T10:05:00:000-0600');
$results['data']['nested'][] = array('Date1' => '2013-08-09T09:00:00:000-0600');
$results['data']['nested'][] = array('Date2' => '2013-08-10T09:30:00:000-0600');
$results['data']['nested'][] = array('Date3' => '2013-08-11T09:45:00:000-0600');
$results['data']['nested'][] = array('Date4' => '2013-08-12T10:05:00:000-0600');

Output: 输出:

Array(
    [info] => Array(
            [0] => some
            [1] => info
            [2] => here
        )
    [data] => Array(
            [0] => Array(
                    [Date1] => 1376049600
                )
            [1] => Array(
                    [Date2] => 1376137800
                )
            [2] => Array(
                    [Date3] => 1376225100
                )
            [3] => Array(
                    [Date4] => 1376312700
                )
            [nested] => Array(
                    [0] => Array(
                            [Date1] => 1376049600
                        )
                    [1] => Array(
                            [Date2] => 1376137800
                        )
                    [2] => Array(
                            [Date3] => 1376225100
                        )
                    [3] => Array(
                            [Date4] => 1376312700
                        )
                )
        )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM