Skip to main content
Inspiring
Apr 1, 2013 11:02 PM UTC
Answered

Need to add 8 hours to a MySQL DATETIME

I'm using PHP and I need to add 8 hours to a DATETIME column in my DB. I want to be able to create a time limit for a user to edit an entry they've entered in my DB.

For example, I would like to hide a button [something like: show if datetime <= NOW()] which prevents me going update page once the deadline has expired...

The code I have in my insert behaviour is this:

.$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s').

and I am trying to create something like this:

.$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s' + 'INTERVAL 8 HOURS').

or

.$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s +8 HOURS').

but I am just getting records with 0000-00-00 00:00:00

If I use the following code, then the current time is posted, not 8 hours in front:

.$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s, INTERVAL 8 HOURS').

Any thoughts as to what I am doing wrong? In fact, if you've got any thoughts on another way I can attack it, then I'd welcome those suggestions too. I did think about using a timestamp and then: "saying show if TIMESTAMP <= 8 hours old" but my computer don't get plain English, and I obvoiulsy don't talk good computer!

Thanks.

This topic is closed to new replies. Start a new post to keep the conversation going.
David_Powers
David_PowersCorrect answer
Inspiring
Apr 1, 2013 11:39 PM UTC

You're trying to mix PHP functions with MySQL ones. This should do the trick:

$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s', strtotime('+ 8 hours'));

2 replies

David_Powers
David_PowersCorrect answer
Inspiring
Apr 1, 2013 11:39 PM UTC

You're trying to mix PHP functions with MySQL ones. This should do the trick:

$_POST['fld_vLOCKDATE'] = date('Y-m-d H:i:s', strtotime('+ 8 hours'));

Translate
Inspiring
Apr 2, 2013 12:01 AM UTC

Thank you David. Much appreciated.

Translate