1

The result is always 0000-00-00 00:00:00 everytime I post a feed

$sql = "INSERT INTO feed ( feed, created, user_id_fk) VALUES (:feed,:created,:user_id)";
        $stmt = $db->prepare($sql);
        $stmt->bindParam("feed", $feed, PDO::PARAM_STR);
        $stmt->bindParam("user_id", $user_id, PDO::PARAM_INT);
        $created = time();
        $stmt->bindParam("created", $created, PDO::PARAM_INT);
        $stmt->execute();
John Doe
  • 83
  • 8
  • did you tried `echo`ing the `created` variable. I you should use `date()` instead of `time()` because according to PHP manual `time()` returns the _seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)_. – S4NDM4N Aug 30 '17 at 03:53
  • I believe your parameters in the `$stmt->bindParam()` require `:` as well, so `$stmt->bindParam(":feed", $feed, PDO::PARAM_STR);`, etc. – Rasclatt Aug 30 '17 at 03:53
  • @Rasclatt but the feed and user_id is already working. Only the created shows 0000... – John Doe Aug 30 '17 at 03:59
  • perhaps just: `$sql = "INSERT INTO feed ( feed, created, user_id_fk) VALUES (:feed,NOW(),:user_id)";` no binding required –  Aug 30 '17 at 04:00
  • What's the field type of the created column? Hunch is it's a date time column rather than a unix time stamp. If so see here: https://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql and you'll need to bind as a string. – Progrock Aug 30 '17 at 04:54

1 Answers1

1

I think it's better to use date() instead of time(). I would like to suggest something like this to you.

//Get time
date_default_timezone_set("America/Adak"); //Set the correct time zone here.
$created = date("Y-m-d H:i:s"); //Output looks like 2001-03-10 17:16:18

$sql = "INSERT INTO feed ( feed, created, user_id_fk) VALUES (:feed,:created,:user_id)";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(":feed", $feed, PDO::PARAM_STR);
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);        
        $stmt->bindParam(":created", $created, PDO::PARAM_STR);
        $stmt->execute();

If you need to know the supported time zones then here is the link

S4NDM4N
  • 904
  • 2
  • 11
  • 26