1

I have an array just like this:

Array
(
   [0] => Array
       (
           [NO] => 1
           [NO_RINCIAN] => 70
           [TANGGAL_PENARIKAN] => 23-DEC-18
       )
   [1] => Array
       (
           [NO] => 2
           [NO_RINCIAN] => 90
           [TANGGAL_PENARIKAN] => 23-Nov-18
       )
 )

What I want to do is to convert TANGGAL_PENARIKAN from date to timestamp but in a whole array.

My expected array:

Array
(
  [0] => Array
      (
         [NO] => 1
         [NO_RINCIAN] => 70
         [TANGGAL_PENARIKAN] => 1545551313
      )
  [1] => Array
      (
         [NO] => 2
         [NO_RINCIAN] => 90
         [TANGGAL_PENARIKAN] => 1542959313
      )
 )

I've tried converting TANGGAL_PENARIKAN inside the array, but I didn't know how to combine to be an array again after it.

public function getAllData()
{

    $data = $this->model_app->getRincian();
    foreach ($data as $key => $value) {
        for ($i=0; $i <= count($value); $i++) { 
            $key = $value['TANGGAL_PENARIKAN'];
        }
        $key = DateTime::createFromFormat("j-M-y", $key);
        print_r(json_encode($key->getTimestamp()));
    }
}

print_r(json_encode($key->getTimestamp()));

Last code above still return :

15455513131542959313

Any well thought to advise will be appreciated.

Thanks.

5 Answers 5

1

Loop your array by reference, and modify the column in your loop.

public function getAllData()
{
    $data = $this->model_app->getRincian();
    foreach ($data as $key => &$value) {
        $timestamp = DateTime::createFromFormat("j-M-y", $value['TANGGAL_PENARIKAN'])->getTimestamp();
        $value['TANGGAL_PENARIKAN'] = $timestamp;
    }
    return $data;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It will convert the date into unix timestamp I have used pre defined function strtotime(String $date)

public function getAllData()
{
    $data = $this->model_app->getRincian();
    foreach ($data as $key => $value) {
        for ($i=0; $i <= count($value); $i++) { 
            $key = strtotime($value['TANGGAL_PENARIKAN']);
        }
        $key = DateTime::createFromFormat("j-M-y", $key);
        print_r(json_encode($key->getTimestamp()));
    }
}

Comments

1

You can try this, just add & symbol before the value in foreach to modify the original array

$array = [
   [
    'no' => 1,
    'NO_RINCIAN' => 70,
    'TANGGAL_PENARIKAN' => '23-DEC-18',
   ],
   [
    'no' => 2,
    'NO_RINCIAN' => 90,
    'TANGGAL_PENARIKAN' => '23-Nov-18',
   ],
];

// notice the & symbol before $value
foreach ($array as $key => &$value) {
   $date = DateTime::createFromFormat("j-M-y", $value['TANGGAL_PENARIKAN']);
   $value['TANGGAL_PENARIKAN'] = $date->getTimestamp();
}

var_dump($array);

You can learn more about passing value by reference here .

Comments

1

Format of your dates can be used for strtotime function, something as

$result = array_map(function($x) { 
        $x['TANGGAL_PENARIKAN'] = strtotime($x['TANGGAL_PENARIKAN']); return $x;},
     $arr);

Comments

0

Use the following code. It will return perfect timestamp value. It simple and easy..

I think it will help you..

<?php
$array = [
   [
    'no' => 1,
    'NO_RINCIAN' => 70,
    'TANGGAL_PENARIKAN' => '23-DEC-18',
   ],
   [
    'no' => 2,
    'NO_RINCIAN' => 90,
    'TANGGAL_PENARIKAN' => '23-JUL-18',
   ],
];
foreach ($array as $key => &$value) {
   $date_timestamp = strtotime($value['TANGGAL_PENARIKAN']);
   $value['TANGGAL_PENARIKAN'] = $date_timestamp;
}
echo "<pre>";
print_r($array);
?>

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.