Logo
Since 1998

A lot more than 50 shades of grey: a time to colour PHP function

Sometimes you just want to give a bit of life to a block of a web page.  We all know what geolocation can do, but these functions are relatively recent, and mostly used to supply related contents for marketing purposes.
What I suggest here is a function that returns an HTML grayscale colour depending of the time of the day and the geographical coordinates of a location you set.  I personally used this code to display a bargraph of daily statistics with different shades depending on the time of the day.

<?php
/*
@Synopsis
This function returns a grayscale time HTML colour (i.e. 3 bytes preceded by #).
I tried to choose a intuitive match for the colours: night is black, day is white,
dusks are a shade of grey, following a sine function that has a full span over the dusk length.
I originally thought of a scale of shades of blue, but by comparing various pictures of the sky I've taken
at different times of the year, I've noticed all the blues are different, 
depending mostly on the latitude, the weather and the time of year.

@Arguments
@Argument1: <t>, a Unix timestamp, leave empty for time().
@Argument2: <longitude>, in decimal format, conventionally signed as positive Eastwards and negative Westwards.
0 being the Greenwich Meridian.
Default has been set to the longitude of Paris.
@Argument3: <latitude>, in decimal format, conventionally signed as positive Northwards and negative Southwards.
0 being the Equator.
Default has been set to the latitude of Paris.

Examples of use:

setlocale(LC_TIME, 'English_Australia.1252'); // Tells strftime to use the proper locale
$details = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
printf ('<div style="background-color: %s;">Welcome to my website, the current date is %s</div>',
    GetTimeColor($sometime = null, $details['longitude'], $details['latitude']),
    strftime('%A %d %B %Y, %H:%M');
);

can return

<div style="background-color: #ffffff;">Welcome to my website, the current date is Saturday 17 November 2012, 11:38</div>
(needs geoip activated).

(c) 2012 Fabien Haddadi
*/

function GetTimeColor($t null$longitude 2.3470$latitude 48.8742) {

    if (
is_null($t)) $t time();
    
$infos date_sun_info($t$latitude$longitude); // Paris

    /* sum of dusk lengths */
    
$dusksLength $infos['sunrise'] - $infos['astronomical_twilight_begin']; // symetrical with evening dusk

    /* full day */
    
if ($t >= $infos['sunrise'] && $t <= $infos['sunset']) {
        
$color 255;
    }
    
    
/* morning dusk */
    
elseif ($t >= $infos['astronomical_twilight_begin'] && $t $infos['sunrise']) {
        
/* it is a colour that is proportional to the sine of the time spent from start of the 
astronomical twilight to now above the total length of dusks. */
        
$color 255 sin(($t $infos['astronomical_twilight_begin']) / $dusksLength pi() / 2);
    }

    
/* evening dusk */
    
elseif ($t $infos['sunset'] && $t <= $infos['astronomical_twilight_end']) {
        
$color 255 * (1-sin(($t $infos['sunset']) / $dusksLength pi() / 2));
    }

    
/* full night */
    
else {
        
$color 0;
    }

    
$colorbyte sprintf('%02x'$color);
    return 
'#' str_repeat($colorbyte3);
}
?>

How did I obtain this spectrum?

I created a loop from midnight to midnight, with a 1 minute interval.  GetTimeColor($t) returns an HTML colour, used as the background-color CSS attribute of a spectrum formed by 1px wide left floating div elements. The result originally has 1440 1px bands (= the number of minutes in a day), so is 1440 px wide, but I reduced it here for publishing purposes.

This is the code that generated the spectrum:


<?php
for($t 0$t <= 24$t += 1/60) {
    
printf ('<div style="float:left; background-color: %s; width: 1px; height: 60px;"></div>',     
        
GetTimeColor(mktime($t, (60*$t)%60)));
}
?>
(740 views)
created 18 November 2012
revised 18 November 2012 by
Would you accept our confidentiality policy? It is about cookies and personal data...