Lee Hamilton
Lee Hamilton

Reputation: 60

How to add 2 hours to variable in php

I am trying to add 2 hours to $server_time and call it $hourPlusTwo. Everything i have tried ends up as either something like 7200 or some obscure date from 1969. How would you do it with what i have here, or rewritten a completely different way? Please understand i am new to php and programming in general. I am trying to understand how to do it, what would be better, and why it works.Thanks in advance.

date_default_timezone_set('America/Los_Angeles');
require_once('mysql_connection.php');
//analyse data by variable time period.
$hour_position = 45;
$htime = -30;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);
$qry = "SELECT `last`,`server_time`,`vol` FROM `btce_btc_ticker` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) = $hour_position ORDER BY `server_time` ASC";
$price_history_qry = mysqli_query($con,$qry);
while($result = mysqli_fetch_array($price_history_qry)){

    $server_time = $result['server_time'];
    $server_time = date("m-d-Y (H:i)",strtotime($server_time));
    echo 'Server Hour ='.$server_time.'<br>';

    echo 'Two Hour ='.$hourPlusTwo.'<br>';
}

Upvotes: 0

Views: 97

Answers (1)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

You have your time format like this date('Y-m-d (H:i)

Whatever the time you got from the server you just need to speccify your format and add +2 hour from it.

The Change would be

$YourNewDate = date('Y-m-d (H:i)', strtotime('+2 hour'));

And the Result would be 2015-04-05 (23:05) some value like this format.

Update :

As you want to do the increment from the time you have from db

<?php  
$result['server_time'] = '2014-04-18 19:56:00';
$server_time = $result['server_time'];
$hourPlusTwo = DateTime::createFromFormat("Y-m-d H:i:s", $server_time);
echo date('Y-m-d (H:i)', strtotime('+2 hours', $hourPlusTwo->getTimestamp()));
?>

Upvotes: 1

Related Questions