2

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.

1
  • 1
    $date = strtok($timestamps[$i], 'T'); Commented May 23, 2015 at 1:38

4 Answers 4

3

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/>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Again two foreach and one more variable assignment. isn't it complexion of the code?
It's just an example. I'm showing that whatever processing can be done at a different time.
You can simply use print_r($date) to print $date.
It's a 10 line example. OP can do whatever they choose to do with the array.
Both approaches work as I want, but I think this is more suited for what I'm doing with my code. Thanks guys!
2

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.

3 Comments

He has an expected, regular format. Why bring the extra overhead of date and strototime into this?
Check the answers @Crayon Violent. you will get the purpose for doing this. thanks.
@anantkumarsingh no, I get it. You think it will make it easier in case format change. Have you considered that the value itself may change, depending on the server's date/time(zone) settings? It's better to just parse the string to ensure the value is preserved
1

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
)

4 Comments

It's not the best option, because date and time format varies. A easy and suitable for all conditions answer is useful. thanks for your effort also.
Of course. This is solution to time format supplied in the OP's question.
It's not about just resolve the issue, it's about give correct and complete solution. Your effort is great.
The date format here in coming for a XML file and is fixed so I don't think solution needs to address all the date format types.
1

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
)

3 Comments

I agree with this solution. Since the date format here is fixed I don't think strotime() is needs and should be used here to reformat it. That was the reason I used preg_replace() in my solution and I got down voted -_-
@Ulver you got downvoted by a jackass repfiend (you know who you are) trying to push common sense down and his own answer up. I gave you an upvote to offset it
My intention is not to disgrace anyone. Sorry for my wordings if it hearts you guys.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.