sun-times/calculate.php

<?php

include('eh.php');
header('content-type: application/json');
ob_start('ob_gzhandler');


// Defaults
$defaults = array
(
	'date' => 'next friday',
	'latitude' => 59.949444,
	'longitude' => 10.756389,
	'timezone' => 'UTC',
	'format' => 'H:i',
);

// Get parameters to use (defaults + user post)
$params = array_intersect_key($_POST, $defaults) + $defaults;
extract($params, EXTR_SKIP);



// Set TimeZone
date_default_timezone_set($timezone);


// Get sun info
$times = date_sun_info(strtotime($date), $latitude, $longitude);


// Format dates
function format_date(&$value, $key, $format)
	{
		if($value !== TRUE && $value !== FALSE)
			$value = date($format, $value);
			
	};
array_walk($times, 'format_date', $format);


// Output result
$params['realdate'] = date(DATE_W3C, strtotime($date));
$result = array
(
	'success' => TRUE,
	'now' => date($format),
	'times' => $times,
	'params' => $params,
);

echo json_encode($result);