learningbyexample learningbyexample - 1 year ago 88
MySQL Question

How do I add hours to a timestamp type field from mysql database

I want to add an hour to a timestamp format in php

this is what I have been trying this whole time:

in my database I have a field that's called ts, as in timestamp (it's a timestamp field that captures

then I have in codeigniter a foreach to show my results. within my results I echo the ts variable but the hour that it captures is wrong so I want to change the time and add 2 hours.

this is the code that I have tried:

foreach($row as $reg){
$timestamp = $reg->ts;
$date = new DateTime($timestamp);
$nTime = $date->modify('+2 hour');
echo $nTime;

it gives me this error:

A PHP Error was encountered Severity: 4096Message: Object of class DateTime could not be converted to stringFilename: libraries/Table.phpLine Number: 353

Answer Source

Minor error in the use of the DateTime object.

foreach($row as $reg){
    $date = new DateTime();
    $date->modify('+2 hour');
    echo $date->format('Y-m-d H:i:s');

You may want to use a different format for the date and time!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download