简体   繁体   中英

Split strings into an array of dates

I have an array of timestamps that I'm importing from different XML files. 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:

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.

$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?

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. 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

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:

$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. 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! 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. 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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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