Beqa
Beqa

Reputation: 99

Remove time expressions from a flat array of datetime strings

I was wondering is it possible to convert the following array:

Array (
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
)

Into this:

Array (
    "2016-03-03",
    "2016-03-03",
    "2016-05-03"
)

Without creating any loops?

Upvotes: -5

Views: 1201

Answers (4)

mickmackusa
mickmackusa

Reputation: 48031

Yes, you can call substr_replace() without a loop or calling array_map(). Truncate the strings from the 10th position. Demo

$array = [
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
];

var_export(
    substr_replace($array, '', 10)
);

Output:

array (
  0 => '2016-03-03',
  1 => '2016-03-03',
  2 => '2016-05-03',
)

You can also count forward from the back of the string. Demo

var_export(
    substr_replace($array, '', -9)
);

Upvotes: 0

pid
pid

Reputation: 11607

Yes, use map:

function first10($s) {
    return substr($s, 0, 10);
}

$result = array_map("first10", $yourArray);

WARNING: this is a good solution only if you are sure that the date format does not change, in other words the first 10 characters must contain the date.

Upvotes: 2

Memor-X
Memor-X

Reputation: 2970

Praveen Kumar's answer is probably the better solution but there is a way to do with what wouldn't really been seen as a loop. instead you use recursion

function explodeNoLoop($array,$delim,$index=0)
{
    $returnArr = array();
    if(isset($array[$index]))
    {
        $expldoed = explode($delim,$array[$index]);
        array_push($returnArr,$expldoed[0]);
    }
    if(isset($array[$index+1]))
    {
        $returnArr = array_merge($returnArr,explodeNoLoop($array,$delim,$index+1));
    }
    return $returnArr;
}

$myArr = array (
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
);

var_dump(explodeNoLoop($myArr," "));

example

How this code works is that with the function we explode the array at the index provided by the function parameter and add this to our returning array. Then we check if there is a value set at the next index which is +1 of the index we passed into the function. If it exists then we call the function again with the new index with the same array and delimiter. We then merge the results of this with our returner array and then return that.

However, with this one should be careful of nest level errors where you excursively call a function too many times, like looking into the reflection of a mirror in a mirror.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167210

There's no explicit loops, if you can use array_map, although internally it loops:

function format_date($val) {
  $v = explode(" ", $val);
  return $v[0];
}
$arr = array_map("format_date", $arr);

From the PHP Manual:

array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map().

Also, when you are dealing with Dates, the right way to do is as follows:

return date("Y-m-d", strtotime($val));

The simple way, using loops is to use a foreach():

foreach($arr as $key => $date)
  $arr[$key] = date("Y-m-d", strtotime($date));

This is the most simplest looping way I can think of considering the index to be anything.


Input:

<?php
$arr = array(
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
);
function format_date($val) {
  $v = explode(" ", $val);
  return $v[0];
}
$arr = array_map("format_date", $arr);

print_r($arr);

Output

Array
(
    [0] => 2016-03-03
    [1] => 2016-03-03
    [2] => 2016-05-03
)

Demo: http://ideone.com/r9AyYV

Upvotes: 9

Related Questions