Ivan
Ivan

Reputation: 1314

Offset date&time result by 3 hours

I have this PHP code which queries the database:

  $result = mysqli_query($con,"SELECT * FROM `ur_destination` WHERE `destination_name` IN ($id)");

echo "<table class='datagrid'>
<tr>
<td>Start date & time GMT</td>
<td>End date & time GMT</td>
</tr>
";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['active_period_start_time'] . "</td>";
  echo "<td>" . $row['active_period_end_time'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

Result looks like this: 2014-04-27 10:50:00 So how can I add +3 hours offset to the result of both start & end time? Thanks.

Upvotes: 1

Views: 69

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18598

Try this

{
  echo "<tr>";
  echo "<td>" . date("Y-m-d H:i:s",strtotime("+3 hours",strtotime($row['active_period_start_time']))) . "</td>";
  echo "<td>" . date("Y-m-d H:i:s",strtotime("+3 hours",strtotime($row['active_period_end_time']))). "</td>";
  echo "</tr>";
}

Upvotes: 1

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27305

If you have an Datetime object you can calculate the dates with the modify operator.

So you can say:

$datet = new \Datetime();
$datet->modify('+3 hours');

Then you have in $datet the time +3 hours. Perhaps you should first read a bit about the date functions in PHP.

Or you use MySQL to calculate the fields in your where clause.

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Upvotes: 0

Related Questions