Barand Posted February 20, 2013 Share Posted February 20, 2013 It does change by date - $inc = new DateInterval('P1D'); $dp = new DatePeriod(new DateTime(), $inc, 20); echo '<pre>'; foreach ($dp as $day) { $t = $day->getTimestamp(); extract(date_sun_info($t, 52,-2)); printf("%s\t%s\t%s\n", $day->format('d M'), date('H:i', $sunrise), date('H:i', $sunset)); } echo '</pre>'; /**** results ******** 20 Feb 07:11 17:32 21 Feb 07:09 17:33 22 Feb 07:06 17:35 23 Feb 07:04 17:37 24 Feb 07:02 17:39 25 Feb 07:00 17:41 26 Feb 06:58 17:43 27 Feb 06:56 17:44 28 Feb 06:54 17:46 01 Mar 06:51 17:48 02 Mar 06:49 17:50 03 Mar 06:47 17:52 04 Mar 06:45 17:53 05 Mar 06:42 17:55 06 Mar 06:40 17:57 07 Mar 06:38 17:59 08 Mar 06:36 18:00 09 Mar 06:33 18:02 10 Mar 06:31 18:04 11 Mar 06:29 18:06 12 Mar 06:27 18:08 */ Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1413709 Share on other sites More sharing options...
MrTIMarshall Posted February 20, 2013 Author Share Posted February 20, 2013 Ah, thank you Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1413712 Share on other sites More sharing options...
MrTIMarshall Posted February 22, 2013 Author Share Posted February 22, 2013 Hiya Barand, I don't suppose you could further help a little as I'm not sure how to make something dynamic where it'll change without needing a page refresh, I gather it needs loads of if statements to check what percent then show the correct colour? I'll watch some PHP tutorials and figure out the the colours and whatnot in the meanwhile to hopefully hear back from you. Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414181 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 It's just a matter of looping through an array of colours. Here's a rewritten section of the code as an example if ($nighttime) { $bgcols = array ( 20 => '#CCC', 40 => '#888', 60 => '#333', 80 => '#888', 100 => '#CCC' ); foreach ($bgcols as $pc => $color) { if ($percent <= $pc) { $bg = $color; break; } } $fg = '#FFF'; } else { $bgcols = array ( 20 => '#BAF', #A0ACFE 40 => '#56F', #5166FD 60 => '#02F', #0220F0 80 => '#56F', 100 => '#BAF' ); foreach ($bgcols as $pc => $color) { if ($percent <= $pc) { $bg = $color; break; } } $fg = '#FFF'; } Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414201 Share on other sites More sharing options...
MrTIMarshall Posted February 22, 2013 Author Share Posted February 22, 2013 Once again, thank you Barand. I really do need to research PHP more as although I understand what the above does and how to implement more colours, I do not know where this code goes exactly and how I use it to edit the colour, I understand that the above picks the colour, but not how it edits the css, presumably I do something like this? <?php //Your code here... <div class="bla" style="background: 'SOME CODE HERE?' ;"> </div> ?> My greater goal I now understand that I will need to learn a lot more about PHP to be able to do so, therefore I have set my standards lower to just do the sky with the moon and sun showing rotating across the screen and the sky colour changing. Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414204 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 Either <?php //Your code here... <div class="bla" style="background-color: $bg;"> </div> ?> or predifine the classes and use classnames instead of color values in the arrays Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414211 Share on other sites More sharing options...
MrTIMarshall Posted February 22, 2013 Author Share Posted February 22, 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>Untitled Document</title> <style> body { margin:0; padding:0; } </style> </head> <body> <?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'; } if ($nighttime) { $bgcols = array ( 20 => '#CCC', 40 => '#888', 60 => '#333', 80 => '#888', 100 => '#CCC' ); foreach ($bgcols as $pc => $color) { if ($percent <= $pc) { $bg = $color; break; } } $fg = '#FFF'; } else { $bgcols = array ( 20 => '#BAF', #A0ACFE 40 => '#56F', #5166FD 60 => '#02F', #0220F0 80 => '#56F', 100 => '#BAF' ); foreach ($bgcols as $pc => $color) { if ($percent <= $pc) { $bg = $color; break; } } $fg = '#FFF'; } echo "<div style='width:100%; height:100px; text-align:center; border: 0; background-color:$bg; color:$fg; padding:0; margin: 0;'>"; printf ("Today, sunrise is %s, sunset is %s", date('g:ia', $todayInfo['sunrise']), date('g:ia', $todayInfo['sunset'])); echo "<br /> $percent%</div>"; ?> </body> </html> I've pieced together the code you've provided, edited it slightly which results in the following page http://sunlightgardenservices.co.uk/SkyTest2.php Has how I've pieced things together correct? Edited February 22, 2013 by MrTIMarshall Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414219 Share on other sites More sharing options...
MrTIMarshall Posted February 23, 2013 Author Share Posted February 23, 2013 I'm guessing that I have gone wrong somewhere as the div constantly says 40% and hasn't changed colour... Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414359 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 I'm guessing that I have gone wrong somewhere as the div constantly says 40% and hasn't changed colour... Sounds like you have Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414382 Share on other sites More sharing options...
MrTIMarshall Posted February 23, 2013 Author Share Posted February 23, 2013 Sounds like you have Ahhh! I feel stupid,or maybe when i tried it was was just a little too tired... All seems to be working now, I just need to change the colours and add more Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414401 Share on other sites More sharing options...
MrTIMarshall Posted February 23, 2013 Author Share Posted February 23, 2013 For 0% to X%, should, for example if I wanted to add a dawn colour, should I place this colour under the 100% on the night colours? Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414407 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 A colour before sunrise will go in the nighttime array, after sunrise in the other Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414408 Share on other sites More sharing options...
MrTIMarshall Posted February 23, 2013 Author Share Posted February 23, 2013 $bgcols = array ( 0 => '#26538D', // Dusk Sky Blue 20 => '#CCC', 40 => '#888', 50 => '#2F2F4F', // Mightnight Blue 60 => '#333', 80 => '#888', ); Is the above the correct way to do it? 0 being the first colour of the night. I'm currently trying to find the colours to tween, to change every 5% or so. Best Regards, Tim Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414409 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 percentage keys need to run from 5 to 100 Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414411 Share on other sites More sharing options...
MrTIMarshall Posted February 23, 2013 Author Share Posted February 23, 2013 I'm a little confused how this works. I had set something for 98% to see if it was working dynamically as the percentage does not change without a refresh. When I set this for 98%, the colour of the div changed even though it was not yet at 98%. I am just playing around with it at the moment, trying to get to grips and noticed this.,, Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414442 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 It loops through the array until it comes to a key value that is greater then the current percent value and uses that colour value Quote Link to comment https://forums.phpfreaks.com/topic/274586-dynamic-changing-website-after-fetching-data-from-external-website/page/2/#findComment-1414462 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.