3

I need to remove some character from string search by value using PHP. I am explaining my code below.

Current Data

$datArr = array(
    array(
        "name" => "Ram",
        "id" => 1,
        "date" => "12/04/2017 05:31:57 AM"
    ),
    array(
        "name" => "Rahim",
        "id" => 5,
        "date" => "12/03/2017 12:31:57 PM"
    ),
    array(
        "name" => "Raj",
        "id" => 4,
        "date" => "12/04/2017 05:31:57 PM"
    )
);

Expected Output

$datArr = array(
    array(
        "name" => "Ram",
        "id" => 1,
        "date" => "12/04/2017 05:31:57"
    ),
    array(
        "name" => "Rahim",
        "id" => 5,
        "date" => "12/03/2017 12:31:57"
    ),
    array(
        "name" => "Raj",
        "id" => 4,
        "date" => "12/04/2017 05:31:57"
    )
);

From the above array there is a date field value and I need to remove the AM/PM from all date using PHP.

LF00
  • 27,015
  • 29
  • 156
  • 295
  • 2
    Why, just do it when you output it, or don't put it in there when creating it. – Lawrence Cherone Dec 05 '17 at 05:27
  • 2
    Try foreach loops checking for the keyname 'date' and remove the unwanted part from the value. – DigiLive Dec 05 '17 at 05:27
  • I am doing like this `foreach ($datArr as $key => $value) { /*if (preg_match('(AM|PM)', $value['date']) === 1) { $datArr[$key]['date']=str_replace('search', replace, subject); }*/ if (strpos($value['date'], 'AM')===true) { $datArr[$key]['date']=str_replace('AM','',$value['date']); } if (strpos($value['date'], 'PM')===true) { echo 'PM
    '; $datArr[$key]['date']=str_replace('PM','',$value['date']); } }` . But its not giving the output as per expected.
    –  Dec 05 '17 at 05:37
  • @subhra you can achieve this with a single statement by passing the array of things to be replaced to `str_replace()`. check out my answer for more info. – RamC Dec 05 '17 at 05:44
  • isn't using string functions dangerous?, how do you now differentiate both 5PM and 5AM if you remove them? shouldn't it be converted to military time too if you're going to take out AM and PM – Kevin Dec 05 '17 at 05:59

8 Answers8

4

Use rtrim to remove the tailing chars like this,

$datArr = array_map(function($v){
    $v['date'] = rtrim($v['date'], " APMapm");
    return $v;
}, $datArr);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    @kris-roofe Good solution, can you please explain a bit about the working of `" APMapm"`, Thanks. – RamC Dec 05 '17 at 06:18
  • It list all characters that to be stripped. You can refer to php manual [rtrim](http://php.net/manual/en/function.rtrim.php) – LF00 Dec 05 '17 at 07:47
0
$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));

Traverse the array using foreach loop. when it comes to date value, do the following

convert the date string to datetime object

$time = strtotime($dateKeyValueInArray);

Now set the date format for this timestamp

$newformat = date('Y-m-d',$time);

See the output by using

echo $newformat;

Reference links are Converting string to Date and DateTime https://www.w3schools.com/php/func_date_date_format.asp

Hope it helps :)

ahmednawazbutt
  • 823
  • 12
  • 34
0

You can do it with str_replace() over a foreach loop

Code

foreach ($datArr as &$dat) {
    $dat['date'] = str_replace([' AM', ' PM'], "", $dat['date']);
}

Input

$datArr = array(
    array(
        "name" => "Ram",
        "id" => 1,
        "date" => "12/04/2017 05:31:57 AM"
    ),
    array(
        "name" => "Rahim",
        "id" => 5,
        "date" => "12/03/2017 12:31:57 PM"
    ),
    array(
        "name" => "Raj",
        "id" => 4,
        "date" => "12/04/2017 05:31:57 PM"
    )
);

Output

$datArr = array(
    array(
        "name" => "Ram",
        "id" => 1,
        "date" => "12/04/2017 05:31:57"
    ),
    array(
        "name" => "Rahim",
        "id" => 5,
        "date" => "12/03/2017 12:31:57"
    ),
    array(
        "name" => "Raj",
        "id" => 4,
        "date" => "12/04/2017 05:31:57"
    )
);
RamC
  • 1,287
  • 1
  • 11
  • 15
0

Try foreach loop for this operation

$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));

print_r($datArr);
foreach($datArr as $key=>$value)
{
    $datArr[$key]['date'] = date('m/d/Y H:i:s',strtotime($value['date']));
}

echo "after";
print_r($datArr);

Output of this code

    Array
(
    [0] => Array
        (
            [name] => Ram
            [id] => 1
            [date] => 12/04/2017 05:31:57 AM
        )

    [1] => Array
        (
            [name] => Rahim
            [id] => 5
            [date] => 12/03/2017 12:31:57 PM
        )

    [2] => Array
        (
            [name] => Raj
            [id] => 4
            [date] => 12/04/2017 05:31:57 PM
        )

)
afterArray
(
    [0] => Array
        (
            [name] => Ram
            [id] => 1
            [date] => 12/04/2017 05:31:57
        )

    [1] => Array
        (
            [name] => Rahim
            [id] => 5
            [date] => 12/03/2017 12:31:57
        )

    [2] => Array
        (
            [name] => Raj
            [id] => 4
            [date] => 12/04/2017 17:31:57
        )

)
Bhupendra Mistry
  • 598
  • 3
  • 11
0

Try this. It is a simple and easy to understand example. substr function is used to take a part of the string from starting 0 to length - 2 for excluding AM/PM

foreach($datArr as $key => $value){
    $datArr[$key]['date'] = substr($value['date'], 0, strlen($value['date'])-2);
}
print_r($datArr); 

Happy Coding :-)

Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
0

You can use a regular expression to remove unwanted words by looping through it.

foreach($datArr as &$i)
{
     $i['date']=preg_replace('[AM|PM]',"",$i['date']);
}
print_r ($datArr);

Output

Array
(
[0] => Array
    (
        [name] => Ram
        [id] => 1
        [date] => 12/04/2017 05:31:57 
    )

[1] => Array
    (
        [name] => Rahim
        [id] => 5
        [date] => 12/03/2017 12:31:57 
    )

[2] => Array
    (
        [name] => Raj
        [id] => 4
        [date] => 12/04/2017 05:31:57 
    )

)
Hari Krishnan
  • 2,049
  • 2
  • 18
  • 29
0
<?php
$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));
echo "<pre>";
print_r($datArr);

$a = array();
foreach ($datArr as &$str) {
    $str = str_replace('PM', ' ', $str);
    $str = str_replace('AM', ' ', $str);
    $a[]=$str;
}
print_r($a);
?>

check fiddle

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
0

Just to say my piece, I think you're potentially going to have wrong date times if you're just outright going to remove AM/PM using string functions. If you do that, how will you differentiate 5AM to 5PM? Now you're going to have the wrong data.

If you're taking out the PM in 5, then you'll need to format it to military time (17). To do that, you'll need to convert each time first. You can do that with DateTime:

$datArr = array_map(function($e){
    $dt = new DateTime($e['date']);
    $e['date'] = $dt->format('m/d/Y H:i:s'); // convert me into correct time

    return $e;
}, $datArr);

Sample Output

Kevin
  • 41,694
  • 12
  • 53
  • 70