MrTIMarshall Posted February 17, 2013 Share Posted February 17, 2013 (edited) Hello, This is my first thread on <?php Freaks, however I am by no means a PHP Freak myself due to lack of experience in the field. I will start off by what I want to accomplish, my end goal, and then continue to my theory on how do achieve this. What I am trying to Achieve? I would like to achieve a website which dynamically changes my websites page so content such as the sky colour is shown differently, the moon is shown, the sun is shown and whatnot depending on the GMT. I believe if I can fetch the information of these times from a website so I know the sunrise and sunset, I can mathematically calculate the tween of sky colours and so on. My Theory on How It is Done. First of all, I would need to know how to store information on a fire on my server to improve loading times for the website, the calculations and events and whatnot, if the file already has so much data already calculated, there is no need to keep opening up a connection to an external website and doing all the mathematical calculations. From the likes of TimeAndDate.com, the information can be found on the sunrise and sunset in London, both of these times are in analogue and not digital, therefore their time in seconds needs to be calculated differently; Date Sunrise Sunset Feb 17, 2013 7:10 AM 5:19 PM The calculation in seconds past 00:00 for when the sunrise. var SunRise = (7*3600)+(10*60) The calculation in seconds past 00:00 for when the sunset. Var SunSet = (5*3600)+(19*60) - either take this figure away from 86400 (24 hours in seconds) or add on 43200 (12 hours in seconds) due to the times being in analogue. Now we have the very basic figures needed to make a, somewhat basic colour switch between day and night switch. If we take away the SunSet time from the SunRise time, we will know how long the DayTime period will be. This will be split up into so many values regardless of how much time is in between, longer days will just last longer on certain colours. I plan on doing it like this: Var DayTime = SunSet - SunRise Var DayColour01 = SunRise Var DayColour01 = SunRise + (DayTime/10) Var DayColour01 = SunRise + ((DayTime/10)*2) ...and so on giving us the time of day in seconds when the next colour change will happen if we were to spit it up into 10 segments. This would also allow for the time to be checked upon loading the website, to load the correct colour. The nighttime tween follows the same method with slight differences in calculations, still using the current days sunset time, but the following days sunrise time. I would also explain the theory, however I am unsure whether each one of these calculations would need to start from 0 seconds (the beginning of what seconds start from) or if it will be done by day, if it is done by day, I am not sure whether iff statements are able to be implemented into calculations, ie; if the total number of seconds is greater than 86400, the seconds in a day, 86400 would need to be taken away from the figure to be able to say when the next colour change will happen in the new day. This is the basic start I would like to accomplish, where I would like to continue this dynamic changing website depending on the GMT timezone to show the sun going across the time only starting to appear at the SunRise time and going completely out of view at the SunSet time, the same with the moon, however a little more advanced to show different images depending on how much of the moon is visible, and so on and so fourth. Although for someone to be able to program this for me would be brilliant, this is not always the case so I would just like to ask if you reply to this thread, if you could dumb things down a little for me as although I have little experiences in other programming languages, sometimes I just really cannot understand things until it's shown as a very basic level where at this point I feel stupid for not understanding it in the first place... Lastly, I thank you for reading such a big thread and for any help/and advice in your replies and your time spent in doing so, in advanced. Best Regards, Tim I do apologize for for formatting errors of this thread, I cannot seem to get the bb-codes to work correctly although everything was appearing fine before making this thread live for people to view. Edited February 17, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/ Share on other sites More sharing options...
PaulRyan Posted February 17, 2013 Share Posted February 17, 2013 (edited) Personally, I would run a cron job at the start of each day, to fetch the sunrise and sunset times, then either save the values to a text file, or in a database depending on your needs. Then you could use something like this: <?PHP //### Turn on error reporting error_reporting(E_ALL); ini_set("display_errors", 1); //### Set default timezone date_default_timezone_set('Europe/London'); //### Turn time into seconds [E.G. '7:10 AM' returns 25800] function timeToSeconds($time) { //### Check if it is AM or PM $timeOfDay = strtoupper(substr($time,-2)) == 'AM' ? 0 : 43200 ; //### Explode the time to get hours and minutes $explodeTime = explode(':', substr($time,0, -3)); //### Convert the hours to seconds $hoursToSeconds = (date('g') > $explodeTime[0]) ? (($explodeTime[0])*60*60)+$timeOfDay : (($explodeTime[0])*60*60); //### Convert the minutes to seconds $minsToSeconds = ($explodeTime[1]*60); //### Return the amount of seconds from time given return intval($hoursToSeconds+$minsToSeconds); } //### Convert the sunrise and sunset times to seconds $sunrise = timeToSeconds('7:10 AM'); $sunset = timeToSeconds('5:19 PM'); //### How many stages do you want? $stages = 10; //### Work out how long each stage is in seconds $stageSeconds = (($sunset-$sunrise)/$stages); //### Get the current time in seconds $currentSeconds = (timeToSeconds(date('g:i A'))+intval(date('s'))); //### Calculate the current stage of the day $currentStage = floor(($currentSeconds-$sunrise)/$stageSeconds); //### If the current stage is not valid, make it valid if($currentStage < 0 || $currentStage > $stages) { $currentStage = 0; } $stageColours = array(0 => '#111', //### This is the colour for when the sun has set or has not risen 1 => '#222', 2 => '#333', 3 => '#444', 4 => '#555', 5 => '#666', 6 => '#777', 7 => '#888', 8 => '#999', 9 => '#AAA', 10 => '#BBB'); echo $stageColours[$currentStage]; ?> You would obviously have to substitute the sunrise and sunset times that are hard coded with the values you have saved from where ever you get the sunrise and sunset times from. *Edit: Changed ROUND() to FLOOR() for better results. Edited February 17, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1412919 Share on other sites More sharing options...
Barand Posted February 17, 2013 Share Posted February 17, 2013 (edited) You may want to look at date_sun_info Also check out the datetime class, in particular datetime::diff to calculate daylight length and elapsed time since sunrise Edited February 17, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1412923 Share on other sites More sharing options...
PaulRyan Posted February 17, 2013 Share Posted February 17, 2013 You may want to look at date_sun_info Also check out the datetime class, in particular datetime::diff to calculate daylight length and elapsed time since sunrise Again, you come up trumps! I've never done anything like this before, but now I know where to look, thanks again Barand (: Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1412926 Share on other sites More sharing options...
MrTIMarshall Posted February 17, 2013 Author Share Posted February 17, 2013 Thank you PaulRyan, I currently have a cron job running for something else, of which I have completely forgot how to do and I don't have a clue how my friend set up the xml/json page, however this sounds like a good starting point, I just need to figure out when the website updates and whatnot... I will also look into what you have mentioned Barand, thank you for your reply too! I know it's not usually the case, but I'm oping I can pick up the language fairly quick, however it is hard to read loads of documentation and debugging loads when you don't know a language due to being registered blind/severally-sighted and needing the windows magnifier on at least x500 magnified... In the past I've learnt via going into the deep end first, looking at code, I'm hoping this is not too big as it is something I've wanted to set up for a while now! I'll be checking back at this thread later on to check for any more replies or to update you of any progress as I've stupidly staying up for I think more than 24 hours now and I am really tired... Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1412927 Share on other sites More sharing options...
Barand Posted February 17, 2013 Share Posted February 17, 2013 (edited) Here's an example $info = date_sun_info(mktime(0,0,0), 53.3761, -2.1897); // Cheshire UK $rise = new datetime(date('Y-m-d H:i:s', $info['sunrise'])); $set = new datetime(date('Y-m-d H:i:s', $info['sunset'])); list($daylightHrs, $daylightMins) = explode(':', $set->diff($rise)->format('%h:%i')); list($elapsedHrs, $elapsedMins) = explode(':', $rise->diff(new DateTime())->format('%h:%i')); $daylight = $daylightHrs*60 + $daylightMins; $elapsed = $elapsedHrs*60 + $elapsedMins; $percent = $elapsed*100/$daylight; printf('Rises: %s<br>Sets: %s<br>Day length: %d minutes<br>Day elapsed: %d minutes (%d%%)', $rise->format('H:i'), $set->format('H:i'), $daylight, $elapsed, $percent ); result Rises: 07:21 Sets: 17:24 Day length: 603 minutes Day elapsed: 460 minutes (76%) Edited February 17, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1412929 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 Day elapsed: 460 minutes (76%) What does this mean and thank you, I guess there is no need to set up a cron job and eventually I can learn to personalize this based upon the users location Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413039 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 Day elapsed: 460 minutes (76%) What does this mean and thank you, It's the measure of how far through the day (between sunrise and sunset) you currently are so you can change background tints accordingly Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413043 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 Okay, so I'm guessing from when the results were posted, 460 minutes (76%), this was the amount of time gone by since 00:00? I'm not sure how I can use this, if I were to have everything running to make things static such as when the sunrise and sunset times are, I could use this perfectly, however I am not sure if it can be done for what I have in mind where as, lets say there are 10 different colours for the night and day at all times, in the summer days are longer so these will just be a slower tween, however in the winter it will be much faster as the days are shorter, same goes for the nights. I would like the daytime colour tween to start at sunrise and end at sunset as for the nighttime colours to go from sunset to the next days sun rise. Maybe I can use this but don't understand how to? I don't understand any of the PHP code you have provided, I can work it out but what I mean is I don't think I yet know enough to modify anything you have provided without breaking it. I'll make a page on one of the domains I own and study the results. I also need to learn if there is a way I can find out a rough location of the user on the site to personalize it for them, their sunrise and sunset, possibly moving on to their weather and whatnot. Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413058 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Day and Night</title> </head> <body> <?php $info = date_sun_info(mktime(0,0,0), 53.3761, -2.1897); // Cheshire UK $rise = new datetime(date('Y-m-d H:i:s', $info['sunrise'])); $set = new datetime(date('Y-m-d H:i:s', $info['sunset'])); list($daylightHrs, $daylightMins) = explode(':', $set->diff($rise)->format('%h:%i')); list($elapsedHrs, $elapsedMins) = explode(':', $rise->diff(new DateTime())->format('%h:%i')); $daylight = $daylightHrs*60 + $daylightMins; $elapsed = $elapsedHrs*60 + $elapsedMins; $percent = $elapsed*100/$daylight; printf('Rises: %s<br>Sets: %s<br>Day length: %d minutes<br>Day elapsed: %d minutes (%d%%)', $rise->format('H:i'), $set->format('H:i'), $daylight, $elapsed, $percent ); ?> </body> </html> The following is on my page at http://sunlightgardenservices.co.uk/Dayandnight.php which doesn't seem to work? Edited February 18, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413062 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 (edited) Okay, so I'm guessing from when the results were posted, 460 minutes (76%), this was the amount of time gone by since 00:00? No - that's not what I said nor is it what the code says. It's the percent of daylight burned up between sunrise and sunset It's the measure of how far through the day (between sunrise and sunset) you currently are so you can change background tints accordingly Edited February 18, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413078 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Day and Night</title> </head> <body> <?php $info = date_sun_info(mktime(0,0,0), 53.3761, -2.1897); // Cheshire UK $rise = new datetime(date('Y-m-d H:i:s', $info['sunrise'])); $set = new datetime(date('Y-m-d H:i:s', $info['sunset'])); list($daylightHrs, $daylightMins) = explode(':', $set->diff($rise)->format('%h:%i')); list($elapsedHrs, $elapsedMins) = explode(':', $rise->diff(new DateTime())->format('%h:%i')); $daylight = $daylightHrs*60 + $daylightMins; $elapsed = $elapsedHrs*60 + $elapsedMins; $percent = $elapsed*100/$daylight; printf('Rises: %s<br>Sets: %s<br>Day length: %d minutes<br>Day elapsed: %d minutes (%d%%)', $rise->format('H:i'), $set->format('H:i'), $daylight, $elapsed, $percent ); ?> </body> </html> The following is on my page at http://sunlightgarde...Dayandnight.php which doesn't seem to work? Wow, that just sounds really useful, I'm sorry I misunderstood. How does this work, I mean where is the information coming from exactly and is it possible to also do this for the sunset to sunrise too? Thank you so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413090 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) It won't allow me to edit my reply, I quoted the wrong message, I was meant to quote your last message, which is the following; No - that's not what I said nor is it what the code says. It's the percent of daylight burned up between sunrise and sunset Do you know why, when I added your code to a PHP page on my server it displayed errors but works for you? Edited February 18, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413091 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 (edited) ... and is it possible to also do this for the sunset to sunrise too? just call the function to get sunset then call for the following date to get sunrise. What version of PHP are you using ? date_sun_info() needs 5.1.2 datetime class needs 5.2 edit You don't need the datetime class to do it. I just thought it would be useful to let you know it existed - it's quite useful. Edited February 18, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413092 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 just call the function to get sunset then call for the following date to get sunrise. What version of PHP are you using ? date_sun_info() needs 5.1.2 datetime class needs 5.2 edit You don't need the datetime class to do it. I just thought it would be useful to let you know it existed - it's quite useful. Version of PHP? I don't have a clue how to check that, all I know is that I am on the 1and1.co.uk business package. I'll try and find out. Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413094 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 echo phpversion(); Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413096 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) echo phpversion(); 5.2.17 Edited February 18, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413108 Share on other sites More sharing options...
Christian F. Posted February 18, 2013 Share Posted February 18, 2013 Posting the error message here would be a lot of help, as we can tell you exactly why you're getting it then. Without having to go to the site in question. Anyway, the reason you're getting the above error is because datetime:diff () wasn't added until PHP 5.3. So you'll either need to have your host upgrade the PHP version, or you'll need to work around it. The easiest way to work around it, is to format both times into Unix timestamps and substract: $setTime = $set->format ('U'); $riseTime = $rise->format ('U'); Then you can use date () to properly format the results into hours and minutes. Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413112 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) Rises: 08:18 Sets: 18:26 Day length: 607 minutes Day elapsed: 446 minutes (73%) Bingo! I just phoned my service provider and they told me there's an option in my control panel to choose my PHP version. Edited February 18, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413115 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 Just in case I knocked out a simplified version with out datetime that covers day and night conditions. Should get you started. <?php $lat = 52; $long= -2; // $timenow = time(); // live version $hour = isset($_GET['hour']) ? $_GET['hour'] : 0; $timenow = mktime($hour,0,0); // testing version $today = mktime(0,0,0); $todayInfo = date_sun_info($today, $lat, $long); $nighttime = false; switch(true) { case $timenow < $todayInfo['sunrise']: // is it pre-sunrise // get last night's sunset $nighttime = true; $yesterdayInfo = date_sun_info(strtotime('-1 days', $today), $lat, $long); $percent = round(($timenow - $yesterdayInfo['sunset'] )*100/($todayInfo['sunrise'] - $yesterdayInfo['sunset'])); break; case $timenow > $todayInfo['sunset']: // get tomorrow's sunrise $nighttime = true; $tomorrowInfo = date_sun_info(strtotime('+1 days', $today), $lat, $long); $percent = round(($timenow - $todayInfo['sunset'] )*100/($tomorrowInfo['sunrise'] - $todayInfo['sunset'])); break; default: $percent = round(($timenow - $todayInfo['sunrise'])*100/($todayInfo['sunset'] - $todayInfo['sunrise'])); break; } if ($nighttime) { $bg = '#000'; $fg = '#FFF'; } else { $bg = '#FFF'; $fg = '#000'; } echo "<div style='width:50px; height:30px; text-align:center; border: 1px solid black; background-color:$bg; color:$fg; padding-top:5px; margin-bottom: 30px;'>"; echo "$percent%</div><hr>"; printf ("Today, sunrise is %s, sunset is %s", date('g:ia', $todayInfo['sunrise']), date('g:ia', $todayInfo['sunset'])); ?> <form> Hour <input type="text" name="hour" value="<?php echo $hour?>" size="5"> <input type="submit" name="btnSubmit" value="Test"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413118 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 That is just fantastic! Thank you so much! I don't want to go too far just yet, I need to get this working, I am just wondering if there is code for moon rise and set times and how much of the moon can be seen using the longitude and latitude as well? Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413123 Share on other sites More sharing options...
Christian F. Posted February 18, 2013 Share Posted February 18, 2013 For the moon phase, I think this should help you along. https://github.com/solarissmoke/php-moon-phase Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413126 Share on other sites More sharing options...
MrTIMarshall Posted February 18, 2013 Author Share Posted February 18, 2013 Thank you Christian F. Does the moon cycle apply to the entire world so if it's a half moon for the UK, it's the same for USA, Australia and whatnot? Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413130 Share on other sites More sharing options...
Christian F. Posted February 18, 2013 Share Posted February 18, 2013 Considering that the moon phase is depending upon the position of the moon relative to the earth and the sun, I'd say yes. Sunrise and sunset depends upon the axial tilt of, and thus the position on, the earth. That's why it changes depending upon where you are, in addition to the time of year. Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413132 Share on other sites More sharing options...
MrTIMarshall Posted February 20, 2013 Author Share Posted February 20, 2013 Just in case I knocked out a simplified version with out datetime that covers day and night conditions. Should get you started. <?php $lat = 52; $long= -2; // $timenow = time(); // live version $hour = isset($_GET['hour']) ? $_GET['hour'] : 0; $timenow = mktime($hour,0,0); // testing version $today = mktime(0,0,0); $todayInfo = date_sun_info($today, $lat, $long); $nighttime = false; switch(true) { case $timenow < $todayInfo['sunrise']: // is it pre-sunrise // get last night's sunset $nighttime = true; $yesterdayInfo = date_sun_info(strtotime('-1 days', $today), $lat, $long); $percent = round(($timenow - $yesterdayInfo['sunset'] )*100/($todayInfo['sunrise'] - $yesterdayInfo['sunset'])); break; case $timenow > $todayInfo['sunset']: // get tomorrow's sunrise $nighttime = true; $tomorrowInfo = date_sun_info(strtotime('+1 days', $today), $lat, $long); $percent = round(($timenow - $todayInfo['sunset'] )*100/($tomorrowInfo['sunrise'] - $todayInfo['sunset'])); break; default: $percent = round(($timenow - $todayInfo['sunrise'])*100/($todayInfo['sunset'] - $todayInfo['sunrise'])); break; } if ($nighttime) { $bg = '#000'; $fg = '#FFF'; } else { $bg = '#FFF'; $fg = '#000'; } echo "<div style='width:50px; height:30px; text-align:center; border: 1px solid black; background-color:$bg; color:$fg; padding-top:5px; margin-bottom: 30px;'>"; echo "$percent%</div><hr>"; printf ("Today, sunrise is %s, sunset is %s", date('g:ia', $todayInfo['sunrise']), date('g:ia', $todayInfo['sunset'])); ?> <form> Hour <input type="text" name="hour" value="<?php echo $hour?>" size="5"> <input type="submit" name="btnSubmit" value="Test"> </form> This code does not seem to change for the day, the times seem to stay the same although other websites state that the times change daily appropriately by 2 minutes? Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/#findComment-1413700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.