Jump to content

Dynamic changing website after fetching data from external website


MrTIMarshall

Recommended Posts

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

*/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<!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 by MrTIMarshall
Link to comment
Share on other sites

 $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

Link to comment
Share on other sites

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.,,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.