-1

I am trying to create a simple application that will allow users to search for two locations and the time difference of two locations will be displayed to them.

I can get the two location Offset using the code bellow but I cannot hook it up to the form inputs so the users can choose the locations!

Here is what I have done so far:

 <form id="form1" name="form1" method="post" action="<?php $get_timezone_offset ?>">
  <label>
    <input type="text" class="timezone1"  id="timezone1" name="timezone1" id="timezone1" />
    <input type="text" class="timezone2"  id="timezone2" name="timezone2" id="timezone2" />
    <input name="" type="button" value="submit" />
  </label>
</form>

    <?php
    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( 'Europe/London', 'Asia/Shanghai' );

    // convert offset to hours
    echo $offset/3600;
    ?>

any help would be appreciated.

Simon Presto
  • 167
  • 3
  • 5
  • 14
  • I'm not sure I understand what the problem is. Are you having trouble parsing the form input and feeding it to the `get_timezone_offset` function? – Technoh Aug 21 '13 at 13:42
  • You are explicitly giving values to your function. As you have put Europe against Asia, you will recieve -7. if you want to put the form into it, you should pass $_POST['timezone1'] and 2 as arguements – Royal Bg Aug 21 '13 at 13:43
  • @Technoh, Yes that is correct. basically what I want to do is to remove the 'Europe/London', 'Asia/Shanghai' which are timezone1 and timezone2 and let the users search for the location using the form provided in HTML format. – Simon Presto Aug 21 '13 at 13:44
  • 1
    @SimonPresto if you have used google, you could type "php forms" and one of the first results is referring to PHP's official site: http://php.net/manual/en/tutorial.forms.php . Please, google before ask next time. – Royal Bg Aug 21 '13 at 13:45
  • @RoyalBg, can you please give me a small example if you don't mind? – Simon Presto Aug 21 '13 at 15:27

1 Answers1

0

You could easily remove the input and replace it with a dropdown list of all available timezones. The list of PHP supported timezones is available in the official PHP documentation. Then you could directly compare the values from one list to the other.

Also, see Generating a drop down list of timezones with PHP, where someone made that list for you.

DateTimeZone::listIdentifiers(DateTimeZone::ALL); will also create a list of available timezones.

Community
  • 1
  • 1
Technoh
  • 1,606
  • 15
  • 34
  • well that is my issue. creating a dropdown list was the next stage that i was gonna follow but at the moment i need to know how I can "Compare the Values" be it in the input form or in a dropdown list! – Simon Presto Aug 21 '13 at 14:10