0

I have been trying to resolve this issue and have spent hours to find the reason why this is keep happening with no avail.

I really don't know what I am doing wrong!

I am trying to create a simple time difference using timezones in PHP. I need to allow the users to choose the both locations on the page. for this purpose I am using two dropdown lists which will have all the timezones in them.

Every time I run the code/page on my server, I get this error:

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (origin_tz)' in /home/a1385482/public_html/timediff.php:204

Stack trace:

#0 /home/a1385482/public_html/timediff.php(204): DateTimeZone->__construct('origin_tz')

#1 /home/a1385482/public_html/timediff.php(215): get_timezone_offset('origin_tz', 'remote_tz')

#2 {main} thrown in /home/a1385482/public_html/timediff.php on line 204

Here is the full page code:

<?php
echo "Version: " . phpversion();
?> 

<form method="post" action="">
  <select name="timezone2" id="timezone2" class="timezone2">
    <option value="Pacific/Midway">(GMT-11:00) Midway Island, Samoa</option>
    .. snip ..
</select>
  <select name="timezone1" id="timezone1" class="timezone1">
    <option value="Pacific/Midway">(GMT-11:00) Midway Island, Samoa</option>
    .. snip ..
  </select>
<input type="submit" name="submit" value="send"/>

</form>


<?php
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('origin_tz', 'remote_tz');
    echo $offset/3600;
}

?>

could someone please shed a light on this as I am stuck on this one for almost 8 hours.

8
  • Please stop posting tons of irrelevant code. Reduce it to the minimum necessary to reproduce the problem. Commented Aug 21, 2013 at 18:28
  • 2
    You're calling get_timezone_offset with 2 strings. I don't think 'origin_tz' is a valid timezone. Commented Aug 21, 2013 at 18:29
  • @deceze, WHY is it irrelevant code?! I am using the exact same code (not irrelevant code) and the exact same code produces the error/problem! Commented Aug 21, 2013 at 18:30
  • 1
    Use some common sense judgement, alright? Hundreds of virtually identical HTML elements: no need, a small sample is fine. Actual code that actually does something related to the problem: necessary. Commented Aug 21, 2013 at 18:33
  • 1
    htmlentities should only be used when echoing onto the screen. It's not for sanitizing input. Commented Aug 21, 2013 at 18:36

1 Answer 1

4

You're passing 'origin_tz' and 'remote_tz' to the constructor which aren't valid timezones, see here: http://www.php.net/manual/en/timezones.php

I imagine you want:

$offset = get_timezone_offset($timezone1, $timezone2);
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried that too! and i get Unknown or bad timezone ($timezone1)' in
@Simon Are you passing get_timezone_offset($timezone1, ...) or get_timezone_offset('$timezone1', ...)?! (Note the quotes.)
@deceze, wow that was it... I was passing it with the quotes!!! it works without the quotes.
@Simon Glad it worked. BTW, learn to read error messages and stack traces. The error message and DateTimeZone->__construct('origin_tz') clearly stated that you're passing the value "origin_tz" (and later "$timezone1") in. That should ring alarm bells and enable you to find the error yourself.
@deceze, sure. appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.