Reputation: 463
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.
Upvotes: 1
Views: 1161
Reputation: 109
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);
?>
Upvotes: 0
Reputation: 26153
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);
Upvotes: 1
Reputation: 1424
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 .
Upvotes: 1
Reputation: 26450
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;
}
Upvotes: 1
Reputation: 880
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()));
}
}
Upvotes: 1