First off, i have been struggling for two days non stop to figure out what I need to with this code and still stuck in the square one.
so here I am again and i hope someone shed a light on this issue as my head is exploding.
what I need to do is to get the difference between two values using DateTime in PHP.
The first value is 00:00 which is a set value and never gonna change.
the second value is $offset/3600 * 1 which is a time difference between two timezones.
the code I am using is this:
<?php
if (0 > $offset)
{
// set an object with the current date
$date = new DateTime();
$date->setTime(00, 00);
// the second date
$date2 = new DateTime($offset/3600 * 1);
// apply the diff() method, getting a DateInterval object ($diDiff)
$diDiff = $date->diff($date2) ;
}
echo $diDiff->format("H:i");
?>
the code above echo's this: H:i
and nothing else!
what am i doing wrong?
Thanks in advance.
EDIT:
The $offset comes from here:
if( isset($_POST['submit']))
{
//be sure to validate and clean your variables
$timezone1 = htmlentities($_POST['timezone1']);
$timezone2 = htmlentities($_POST['timezone2']);
//then you can use them in a PHP function.
function get_timezone_offset( $origin_tz, $remote_tz ) {
$timezone1 = new DateTimeZone( $origin_tz );
$timezone2 = new DateTimeZone( $remote_tz );
$datetime1 = new DateTime("now", $timezone1);
$datetime2 = new DateTime("now", $timezone2);
$offset = $timezone1->getOffset($datetime1) - $timezone2->getOffset($datetime2);
return $offset;
}
$offset = get_timezone_offset($timezone1, $timezone2);
}
?>
timezone1 and timezone2 are two dropdown lists with php timezones inside them and they are identical.
like so:
<select name="timezone2" id="timezone2" class="timezone2">
<?php
foreach($options as $key => $value)
{
echo '<option value="'. $key .'" label="'. $value .'">'.$value.'</option>';
}
?>
<option value="Africa/Abidjan" label="Abidjan">Abidjan</option>
<option value="Africa/Accra" label="Accra">Accra</option>
<option value="Africa/Addis_Ababa" label="Addis Ababa">Addis Ababa</option>
<option value="Africa/Algiers" label="Algiers">Algiers</option>
<option value="Africa/Asmara" label="Asmara">Asmara</option>
</select>
With the DateInterval object, you have to prefix all the formatting letters with the "%" character. So use:
echo $diDiff->format("%H:%i");
Happy hunting!