开发者

Add 10 minutes into the future into a MySQL db using PHP/PDO

I want to add a row to a db with a time 'x' amount into the future (different for each row). I have looked throug开发者_运维技巧h every tutorial/help/whatever I could find via google but nothing seems to help.

From what I can tell, the best way is to do it with MySQL.

SELECT ADDTIME(now(), '00:10:00')

But how do I actually make that work with this:

$new_row = $db->prepare("INSERT INTO table (field1, field2, time) VALUES(?, ?,?)");

$new_row->execute(array($field1, $field2, $timeintenminutes)) or dieWithDBError($new_row);


Untested, but have you tried:

$new_row = $db->prepare("INSERT INTO table (field1, field2, time) VALUES(?, ?, ADDTIME(NOW(), '00:10:00'))");
$new_row->execute(array($field1, $field2)) or dieWithDBError($new_row);


What is the column type of time?

In PHP, you can simple do that $AddSomeTime=time() + 60; which will add 1 minute from now.

I presume want that instead of updating the MySQL table? If you want to work with MySQL update query, than use UPDATE YourTable SET valuet=(ADDTIME(now(), '00:10:00')) WHERE id=$id;


$future_time = date("Y-m-d H:i:s",time()+60*10);

This will get you the 10 minute added time.


In Php strtotime() will do the trick for adding values to time or date

$1hrInFuture = strtotime('now +1 hours');

str2time (now); will understand now as the server's current time, and will add 1 hrs to it, you can add +1 weeks, +1 months...etc.

to make it formatted correctly, use the date:

$1hrInFutureF = date('Y-m-d H:i:s',$1hrInFuture);

this example will get the current servers date, and add 1hr to it;

Note: not using 'now' in the strtotime will generate the date 'January 1 1970 00:00:00 UTC';

Now you can get the date from the database, insert it into the strtotime, add 1 hr to it, format it the way you want, and insert it back to a new column.

and for the different timezone between the mysql n the server, you can create a column in that db, set a randdom hash inside the inserted row, the select that row's normal_date, and insert the +1 hr date using the current_time_stamp and the random number

INSERT INTO X (title,rnd_nb) VALUES ('titke','rnd_nb);
SELECT ID FROM X WHERE rnd_nb = $rnd_nb(php variable inside the function);
UPDATE `x` SET (1hrplus_date='$the_variable_containing_the_future_time') WHERE id='the_id_grabbed_from_the_second_query);

So if you see, we inserted a random number there,automaticaly if its the currenttimestamp, it will add the current_time_stamp..etc and selected the row containing that number, got it's id, , then get the currenttimestamp, put it in the strtotime function to add 1 hr to it, update that row, and set the +1hrcolumn using the strtotime output.

Hope that's useful :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜