Convert Unix Timestamp to MySQL DATETIME

It sounds stupid, but for life I can’t understand. I have tried everything.

The problem is that I cannot figure out how to get the date string in my MySQL table ... I first converted to Unix to account for changes in the input format.

$_POST['date'] is in this format: 19-1-2013 4:43:32

 $datestr= strtotime($_POST['date']); echo $datestr; $sql = "INSERT INTO `calendar` (`date`, `title`, `event`) VALUES ('FROM_UNIXTIME(".$datestr.")', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['desc'])."')"; $query = mysql_query($sql); 

This is the last thing I tried, and still getting all the zeros in the DATETIME field.

Please, what am I doing wrong?

+4
source share
1 answer

remove single quotes from the FROM_UNIXTIME function because this will make it a value. eg,

 $sql = "INSERT INTO `calendar` (`date`, `title`, `event`) VALUES (FROM_UNIXTIME(".$datestr."), '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['desc'])."')"; 

As a side element, your query is vulnerable with SQL Injection , see the article below to learn how to protect it.

+11
source

All Articles