-2

Here is my array snapshot

Array(
        [created_at] => 2014-02-18T08:29:33.388Z
        [modified_at] => 2014-02-18T12:44:46.011Z
      )
  1. I have to remove character from [created] array and [modified_at], how should I resolve this issue?
  2. I want like : [created_at] =>2014-02-18 8:29:33
  3. secondly like : [modified_at] =>2014-02-18 12:44:46

How to resolve this issue? Data is in form of json , I have tried this code:

preg_replace('/[^a-zA-Z0-9]/', "_", $aba);

whats is correct method to resolve this issue.

Roopendra
  • 7,674
  • 16
  • 65
  • 92
mohd afeef
  • 35
  • 1
  • 10
  • The code you have tried is completely irrelevant to the problem. Do you really want to "replace anything that's not a letter or number with underscore"? Not according to your question! – Niet the Dark Absol Feb 21 '14 at 09:28
  • i want like 2014-02-18 8:29:33 from [created_at] => 2014-02-18T08:29:33.388Z – mohd afeef Feb 21 '14 at 09:30

4 Answers4

1

Not necessary to use regex, just re format the date string.

Example:

foreach ($arr as $key => $value) {
    if (in_array($key, array('created_at', 'modified_at'))) {
        $arr[$key] = date('Y-m-d H:i:s', strtotime($value));
    }
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • That brings in extra overhead (converting a string to a timestamp with strtotime and then back again with date) just to manipulate the string values and as such doesn't address the question that was asked. – kguest Feb 26 '14 at 08:39
0

To do strictly what you want, which is string manipulation (of strings which just happen to represent date/timestamps):

$t = Array (
    'created_at' => '2014-02-18T08:29:33.388Z',
    'modified_at' => '2014-02-18T12:44:46.011Z'
);

$r = array();
foreach ($t as $key => $element) {
    $r[$key] = str_replace('T', ' ', str_replace('Z', '', $element));

}


var_dump($r);

This doesn't have the overhead of converting strings to datestamps and back again.

kguest
  • 3,804
  • 3
  • 29
  • 31
0
$result = preg_replace('/([0-9\-]+)[^0-9]*?([0-9\:]+).*/', '$1 $2', $array['created_at']);

will give you:

2014-02-18 12:44:46
2014-02-18 08:29:33
Manu
  • 922
  • 6
  • 16
0

Please look this

 $str = "2014-02-18T08:29:33.388Z";
   $nstr = preg_replace('/\.([0-9]{3}[a-zA-Z])$/', "", $str);
   echo preg_replace('/[a-zA-Z]/', " ", $nstr);

OUTPUT: 2014-02-18 08:29:33

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97