Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43
  1. #31
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Great!

    However, there is an issue with your code. If $percent is 100, you will write timePassed = 0.100 to the Javascript, and if $percent is 5, timePassed will be the same as if $percent was 50 – uh-oh!

    Simply replace the line

    Code:
    timePassed = 0.<?php echo "$percent"; ?>,
    with

    Code:
    timePassed = <?php echo $percent / 100; ?>,
    and it's fixed.

  2. Users who have thanked Airblader for this post:

    MrTIMarshall (03-17-2013)

  3. #32
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Airblader View Post
    Great!

    However, there is an issue with your code. If $percent is 100, you will write timePassed = 0.100 to the Javascript, and if $percent is 5, timePassed will be the same as if $percent was 50 uh-oh!

    Simply replace the line

    Code:
    timePassed = 0.<?php echo "$percent"; ?>,
    with

    Code:
    timePassed = <?php echo $percent / 100; ?>,
    and it's fixed.
    I tried going one step better via using;
    Code:
    timePassed = <?php if (!$nighttime) { echo $percent / 100; }; ?>,
    But it never worked. I'm going to leave it for a couple hours or so to see if it dynamically moves or requires a refresh.

  4. #33
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    Hmmm... It's not dynamic and I don't have a clue where to begin...

  5. #34
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    The $nighttime switch will work but only during daytime. Because if it is actually nighttime, you will produce this line of Javascript:

    Code:
    timePassed = ,
    which is invalid syntax and will throw an error. For the second part: What do you mean by dynamic? That the sun automatically moves without refreshing the page? Of course that won't work PHP will only be parsed when it is requested from the server (it's a server-side language) and since the Javascript now doesn't contain any kind of animation code, there won't be any dynamic movement. You'd have to implement that in Javascript.

  6. #35
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    Yes, I did mean for it to move without a refresh. I'm guessing that the PHP code needs to be written in Javascript apart from the variables which can be passed over?

  7. #36
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    One way is to implement everything in Javascript from the beginning, which I would prefer if this is the only context you use that code for and if you don't use anything that's too hard to implement in Javascript.

    Another option would be writing a PHP script that will return the data we need and then use Javascript to make repeated Ajax requests. The reason I like the latter method less is because a) don't mix languages if you don't have to and b) don't waste traffic and your user's bandwidth if you don't have to.

    Looks to me the only non-standard function you're using is date_sun_info(). How is that function implemented?

  8. #37
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    Based upon the users Longitude and Latitude which will be implemented at a later date, this knows the Sunrise/Sunset.

    I am wanting this to be a realistic sky for where ever you are. I am currently working on the on the Moon phrase and then to animate this into the sky too;

    https://tornhq.com/Top_Menu/moonphrase2.php - Month View
    https://tornhq.com/Top_Menu/moonphrase.php - Current Moon

  9. #38
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    How do you plan on obtaining the geolocation of the user? If you're just gonna ask him, you can do that with Javascript, too. But Javascript offers you an even better way (as an addition): The HTML5 API in combination with a modern browser allows you to obtain the user's location if he agrees on it. Wouldn't that be awesome?

    I suggest you think about rewriting your code in Javascript. It's not that much to migrate anyway, for the moment.

  10. #39
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    At this moment in time I have the MaxMind GeoLite City database. Upon launch, I will be purchasing the paid version.

    I have not yet found a locate of the user as good as MaxMind's paid version, whereas every other one don't know where I am.

    ...As for converting what I have into Javascript, they both look the same to me :/

  11. #40
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    mktime in PHP is 1363474800
    PHP Code:
    $today mktime(0,0,0); 
    ...But in Javascript it equals 1363478400
    Code:
    <script type="text/javascript">
    function mktime () {
      var d = new Date(),
        r = arguments,
        i = 0,
        e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];
    
      for (i = 0; i < e.length; i++) {
        if (typeof r[i] === 'undefined') {
          r[i] = d['get' + e[i]]();
          r[i] += (i === 3); // +1 to fix JS months.
        } else {
          r[i] = parseInt(r[i], 10);
          if (isNaN(r[i])) {
            return false;
          }
        }
      }
    
      r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
      d.setFullYear(r[5], r[3] - 1, r[4]);
      d.setHours(r[0], r[1], r[2]);
      return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
    }
    var today = mktime(0,0,0);
    document.write(today);
    </script>

  12. #41
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    https://tornhq.com/Top_Menu/SunTest-Javascript.php

    My slow progress, but apart from the mktime not being the same as the PHP version, I think these are okay?

  13. #42
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Why do you even call mktime like that? Wouldn't you want an exact time rather than the time at midnight? The current UNIX timestamp can easily be obtained in Javascript with

    Code:
    var currentTime = ((new Date()).getTime() / 1000) | 0;
    Another advantage of doing this client-side is that you'll actually have the client's time (given he set it up right) rather than your server's time.

  14. #43
    Regular Coder
    Join Date
    Nov 2010
    Posts
    369
    Thanks
    46
    Thanked 1 Time in 1 Post
    Someone made this for me, I don't know what they done it this way sorry and yes, I would like their local time.


 
Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •