简体   繁体   English

将字符串拆分为日期数组

[英]Split strings into an array of dates

I have an array of timestamps that I'm importing from different XML files. 我有一组时间戳,我从不同的XML文件导入。 This is how they look like: 这是他们的样子:

<field name="timestamp">2015-04-16T07:14:16Z</field>

So I have a bunch of them stored in an array named $timestamps like this: 所以我有一堆存储在名为$ timestamps的数组中,如下所示:

2015-04-16T07:14:16Z
2015-04-24T14:34:50Z
2015-04-25T08:07:24Z
2015-04-30T07:48:12Z
2015-05-02T08:37:01Z
2015-05-09T10:41:45Z
2015-05-01T07:27:21Z
2015-05-07T09:41:36Z
2015-05-12T04:06:11Z
2015-05-12T05:52:52Z
2015-05-12T11:28:16Z

I am only interested in the date part, not the time. 我只对日期部分感兴趣,而不是时间。 I have tried splitting the string using the split() function. 我尝试使用split()函数拆分字符串。

$dates = array();

for ($i=0; $i<count($timestamps); $i++){
        $dates = split ("T", $timestamps[$i]);
        echo $dates[$i] . "<br>"; 
    }

From what I understand it is storing the first part (before the T) then the part after the T. How can it store only the first part of each string? 据我所知,它是存储第一部分(在T之前)然后存储在T之后的部分。它如何只存储每个字符串的第一部分?

When I try this: 当我尝试这个:

echo $dates[1];

it outputs the first date fine. 它输出第一个日期罚款。 I'm not quite sure about the rest. 其余的我不太确定。

Any suggestions on a better way to accomplish this? 有关更好的方法来实现这一点的任何建议吗?

Thanks! 谢谢!

You should use strtotime and date , as opposed to string splitting and/or regex. 您应该使用strtotimedate ,而不是字符串拆分和/或正则表达式。 This will help if your date format ever changes. 如果您的日期格式发生变化,这将有所帮助。

$dates = array();

foreach ($timestamps as $timestamp) {
    $d = strtotime($timestamp);
    $dates[] = date('Y-m-d', $d);
}

foreach ($dates as $date) {
    echo $date . '<br/>';
}

I think splitting is not better the best is get date using date function easily. 我认为拆分并不是更好,最好是使用日期功能轻松获取日期。 Very easy code:- 非常简单的代码: -

<?php
$dates = array('2015-04-16T07:14:16Z','2015-04-24T14:34:50Z','2015-04-25T08:07:24Z','2015-04-30T07:48:12Z','2015-05-02T08:37:01Z'); // i hope your dates array is like this

foreach($dates as $date){
    echo date('Y-m-d',strtotime($date)).'<br/>';
}
?>

Output:- http://prntscr.com/78b0x4 输出: - http://prntscr.com/78b0x4

Note:-I didn't take your whole array. 注意: - 我没有拿走你的整个阵列。 Because it's easy to see and understand what i am doing there in my code. 因为在我的代码中很容易看到并理解我在做什么。 thanks. 谢谢。

You can simply use preg_replace() to remove all the "time" bits in the array: 您只需使用preg_replace()删除数组中的所有“time”位:

$array = Array('2015-04-16T07:14:16Z', '2015-04-24T14:34:50Z', '2015-04-25T08:07:24Z');

// Remove "T" and anything after it
$output = preg_replace('/T.*/', '', $array);
print_r($output);

Outputs: 输出:

Array
(
    [0] => 2015-04-16
    [1] => 2015-04-24
    [2] => 2015-04-25
)

There's no reason to drag date and strotime into this, that's just extra overhead. 没有理由将datestrotime ,这只是额外的开销。 You have an expected, regular format already. 您已经有了预期的常规格式。

And I would also give a warning about using date functions: you may run into trouble with the values changing after you put them through date and strtotime depending on your server's date/time(zone) settings! 而且我也将给予有关使用日期函数警告:你可能会遇到的价值观改变你把他们通过后麻烦datestrtotime取决于你的服务器的日期/时间(区域)设置! Since your strings do not specify the timezone offset, you won't even be able to properly convert.. you'll just have to roll with whatever your server is at or pick one yourself. 由于你的字符串没有指定时区偏移量,你甚至无法正确转换..你只需要随身携带任何服务器或自己选择一个。

The safer way to ensure the actual value doesn't change is to just parse it as a string. 确保实际值不会改变的更安全的方法是将其解析为字符串。 Splitting at the "T" is fine. 拆分“T”很好。 You're just having trouble with how to handle the variables. 你只是遇到如何处理变量的麻烦。 Here is an example: 这是一个例子:

// example data
$timestamps =<<<EOT
015-04-16T07:14:16Z
2015-04-24T14:34:50Z
2015-04-25T08:07:24Z
2015-04-30T07:48:12Z
2015-05-02T08:37:01Z
2015-05-09T10:41:45Z
2015-05-01T07:27:21Z
2015-05-07T09:41:36Z
2015-05-12T04:06:11Z
2015-05-12T05:52:52Z
2015-05-12T11:28:16Z
EOT;
$timestamps=explode("\n",$timestamps);


$dates = array();
for ($i=0; $i<count($timestamps); $i++){
  $d = explode("T", $timestamps[$i]);
  $dates[] = $d[0];
}
print_r($dates);

output: 输出:

Array
(
    [0] => 015-04-16
    [1] => 2015-04-24
    [2] => 2015-04-25
    [3] => 2015-04-30
    [4] => 2015-05-02
    [5] => 2015-05-09
    [6] => 2015-05-01
    [7] => 2015-05-07
    [8] => 2015-05-12
    [9] => 2015-05-12
    [10] => 2015-05-12
)

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

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