Reputation: 5742
I am trying to save a datestring in PHP into a MySQL database as timestamp datatype. I found a lot of posts about this, but none of them worked for me. This is what I've tried:
$date = $_POST['date'];
$timestamp = date("m/d/Y", strtotime($date));
$sql = "insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', '$timestamp', '$id');";
But in the database, I only get:
0000-00-00 00:00:00
The input string from the POST object is 05/30/2013
. Thanks a lot for your support!
Upvotes: 10
Views: 46613
Reputation: 9
We have NOW()
function in MySQL.
insert into sale(service, amount, date, customerid_fk)values('$service', '$amount', NOW(), '$id');
Upvotes: 1
Reputation: 282
SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00'); -- **1111885200**
SELECT FROM_UNIXTIME(1111885200); -- **2005-03-27 03:00:00**
$date = $_POST['date'];
$timestamp = date('Y-m-d H:i:s', strtotime($date));
$sql = "insert into sale(service, amount, date, customerid_fk) values
('$service', '$amount', UNIX_TIMESTAMP('$timestamp'), '$id');"
Upvotes: 0
Reputation: 1
you should use mysql methods
I got this date from a datebox of easyui
08/25/2016
then
$date = "FROM_UNIXTIME(".strtotime(08/25/2016).")";
INSERT INTO TABLE (col1, col2, dateI) values($a, $X, $date);
Upvotes: 0
Reputation: 368
This worked for me:
//timestamp fix
$date = new \DateTime();
$created_at = $date->setTimestamp(intval($timestamp));
Upvotes: 1
Reputation: 4282
This might work for you:
$date = $_POST['date'];
$timestamp = date('Y-m-d H:i:s', strtotime($date));
$sql = "insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', '$timestamp', '$id');";
Upvotes: 27
Reputation: 33
if you have date field type as DATE
or DATE/TIME
it will add timestamps like this. you need to make your date field type as varchar and add your timestamps as strings.
if you need to add date in format d/m/y then mysql has different format like y-m-d
so before adding you can do like this
date('y-m-d', strtotime('your-date-string'))
Upvotes: 0
Reputation: 873
you need to remove apex for put timestamp as long
insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', $timestamp, '$id')
or use this format for date 'Y-m-d H:i:s'
Upvotes: 0