10

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!

4
  • mySQL expects the Y-m-d format for timestamps. Change the format string accordingly and you'll be good. Commented May 9, 2013 at 8:50
  • Did you already check this? stackoverflow.com/questions/2501915/… Commented May 9, 2013 at 8:51
  • stackoverflow.com/questions/7112982/… is what you need i expect. Commented May 9, 2013 at 8:52
  • If you want to pass the parameter as a string then convert it into the DB use STR_TO_DATE(yourstring, '%m/%d/%Y') Commented May 9, 2013 at 8:54

7 Answers 7

27

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');";
Sign up to request clarification or add additional context in comments.

Comments

1

This worked for me:

//timestamp fix
$date = new \DateTime();
$created_at = $date->setTimestamp(intval($timestamp));

Comments

1

We have NOW() function in MySQL.

insert into sale(service, amount, date, customerid_fk)values('$service', '$amount', NOW(), '$id');

Comments

0

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'

Comments

0

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'))

Comments

0

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);

Comments

0
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');"

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.