1

I have an array with date & time like below:

$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');

From each value, the T10:00 should be cut off, so that my new array looks like this:

$new_array = array('2021-05-04', '2021-05-05', '2021-05-06');

How can i do that?

6

3 Answers 3

2
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = [];
foreach($array as $a) {
  $a = explode('T', $a)[0];
  array_push($new_array, $a);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is this ok? hope so.
Awesome. Exactly what i was looking for. Thnx
Thanks a lot. See you.
2

Iterate through the array by array_map with callback function take only first 10 chars, Which represent time.

$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>substr($time, 0, 10), $array);
print_r($new_array);

Prints:

/*
Array
(
    [0] => 2021-05-04
    [1] => 2021-05-05
    [2] => 2021-05-06
)
*/

1 Comment

Great solution in 2 lines of code! Super!
2

the T10:00 should be cut off

If you have a constant time T10:00 and want to get rid of it just replace it with empty!

$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>str_replace('T10:00', '', $time), $array);

print_r($new_array);
//Array ( [0] => 2021-05-04 [1] => 2021-05-05 [2] => 2021-05-06 ) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.