PHP MySQL datetime

October 22, 2010 at 09:39:18
Specs: Windows XP
Hello, i got a problem with inserting a date and time in MySQL Database.

$cur_time=date("d-m-Y H:i:s");
$add_time = date('H') + 12;
$next_time = date("d-m-Y ".$add_time.":i:s");

mysql_query("INSERT INTO votelog (`cur_time`, `next_time`) VALUES('$cur_time', '$next_time')");

cur_time and next_time tables is a DATETIME, and it gives me an error :

Incorrect datetime value: '23-10-2010 20:35:28' for column 'cur_time' at row 1


See More: PHP MySQL datetime

Report •


#1
October 22, 2010 at 09:54:49
DATETIME expects dates to be Y-m-d (like 2010-10-21)

Also - you can do all that right in the query:

INSERT INTO votelog (`cur_time`, `next_time`)
VALUES(
NOW(),
NOW() + INTERVAL 12 HOUR
)
;


Report •

#2
October 22, 2010 at 10:47:30
Thank you, now everything is clear :)

Report •

#3
October 22, 2010 at 12:30:03
But i got a new problem, i need to Subtract to time values, its not working..

// This part works and returns time in DATATIME - Y-m-d H:m:s

$a_get_user_next_time = mysql_query("SELECT * FROM `votelog` WHERE `id`='$user_id2' AND `site`='1'");
$a_user_next_time = mysql_fetch_array($a_get_user_next_time);
$a_user_next_time2 = $a_user_next_time['next_time'];

$a_cur_date = date("Y-m-d H:m:s");

$time_left = $a_user_next_time2 - $a_cur_date;

echo " $time_left ";

And it returns me a "0" .. why is this happening


Report •

Related Solutions

#4
October 22, 2010 at 13:11:37
Because both $a_user_next_time2 and $a_cur_date are strings - you'd need to use time() or strtotime() to make them seconds, and then substract that.

i.e.:

$time_left = strtotime($a_user_next_time2) - time();

echo " $time_left seconds remaining ";


Report •

#5
October 22, 2010 at 13:51:16
Thank you, but this returns me a result : 34001

I gues its a UNIX timestamp, how can i convert it to normal ?? :)
Tried, but it shows wrong.


Report •

#6
October 22, 2010 at 16:35:31
34001 is the number of seconds (about 9 1/2 hours) - here's an example on how to make it human readable:

http://csl.sublevel3.org/php-secs-t...


Report •

#7
October 22, 2010 at 16:51:11
Men this is very complicated, can you please show me an example ? :)

Report •


Ask Question