I am trying to build small API. I am using Laravel.
I have this table for example:
CREATE TABLE `Incomes` (
.......................................
`comment` VARCHAR(255) NULL DEFAULT NULL,
`dateInsert` TIMESTAMP NULL DEFAULT NULL,
`dateUpdate` TIMESTAMP NULL DEFAULT NULL,
`dateDelete` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`guid`),
.......................................
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
I am doing updating of dateUpdate
field on some row:
update `Incomes` set `dateInsert` = '1432111937' where `guid` = 'lalvlldlv1laslf'
After that I have this data in row:
I don't even have timestamp there, just 0000-00-00 00:00:00
.
Why I get so?
Why question about PHP? I am using Laravel to update rows.
I have this code, which generates code above:
$jsonArray = $input->data;
foreach ($jsonArray as $item) {
$item = (object)$item;
$income = Income::find($item->guid);
if($income) {
$income->categoryId = $item->categoryId;
$income->billId = $item->billId;
$income->userId = $item->userId;
$income->amount = $item->amount;
$income->comment = $item->comment;
$income->dateInsert = $item->dateInsert;
$income->dateUpdate = $item->dateUpdate;
$income->dateDelete = $item->dateDelete;
$income->save();
}
}
I parse this json into $jsonArray
variable:
{
"data": [
{
"guid": "lalvlldlv1laslf",
"categoryId": "123e1cs",
"billId": "12312",
"userId": "214123",
"amount": 200,
"comment": null,
"dateInsert": 1432111937,
"dateUpdate": null,
"dateDelete": null
}
]
}
Why I am not getting normal date/normal timestamp in rows?
Timestamp
datatype supports date in Y-m-d H-i-s
format and hence its failing, you need to convert into proper format. In mysql it could be done using from_unixtime
mysql> select from_unixtime('1432111937');
+-----------------------------+
| from_unixtime('1432111937') |
+-----------------------------+
| 2015-05-20 14:22:17 |
+-----------------------------+
1 row in set (0.05 sec)
So the update command will be
update `Incomes`
set `dateInsert` = from_unixtime('1432111937')
where `guid` = 'lalvlldlv1laslf'