<?php
//============================================================================================
// Web service to deliver the server date & time (local and GMT)
//       Receives days:     Optional delta number of days (+/-) to return the date/time for
//                callback: Optional name of a callback function for JSONP
//       Returns: date and time from the server in JSON format:
//                [ {"type":"local","date":"Sunday","year":"value","month":"value", ...}, 
//                  {"type":"GMT",  "date":"Monday","year":"value","month":"value", ...} ]
//  
//===========================================================================================

error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);  //all except warnings & notices

header("Content-type: text/plain");         #send plain text (JSON)
header("Access-Control-Allow-Origin: *");   #allow access from other domains (optional)  

$days     = $_GET['days'];                  #get param from HTML form or query string
$callback = $_GET['callback'];              #get param from query string (for optional JSONP)

if (!$days) $days=0;                        #if none, set to 0

$datetime  = time();                        #get date&time in seconds since 1/1/1970
$datetime += $days*24*60*60;                #add/subtract that many days

$date  = date('l',$datetime); 
$year  = date('Y',$datetime);
$mth   = date('m',$datetime);
$day   = date('d',$datetime);
$hour  = date('h',$datetime);
$min   = date('i',$datetime);
$sec   = date('s',$datetime);
$am_pm = date('A',$datetime);

$gmt = array("type"=>"GMT","date"=>$date,
             "year"=>$year,"month"=>$mth,"day"=>$day,
             "hour"=>$hour,"minute"=>$min,"second"=>$sec,
             "am_pm"=>$am_pm,"tzone"=>"0","time"=>$datetime);

date_default_timezone_set('America/New_York');   #change timezone to NY, default is set to GMT

$date  = date('l',$datetime); 
$year  = date('Y',$datetime);
$mth   = date('m',$datetime);
$day   = date('d',$datetime);
$hour  = date('h',$datetime);
$min   = date('i',$datetime);
$sec   = date('s',$datetime);
$am_pm = date('A',$datetime);

$tzone = date('O',$datetime)/100;           #time zone in hours
$tzSec = date('Z',$datetime);               #time zone is seconds
$time  = $datetime + $tzSec; 

$local = array("type"=>"local","date"=>$date,
               "year"=>$year,"month"=>$mth,"day"=>$day,
               "hour"=>$hour,"minute"=>$min,"second"=>$sec,
               "am_pm"=>$am_pm,"tzone"=>$tzone,"time"=>$time);
               
$data = array($local,$gmt);                 #add both arrays

$json = json_encode($data);                 #convert to json string


if (! $callback)                            #if no callback function is provided
    print $json;                            #send/return the json string 
else
    print $callback .'( '. $json .' )';     #send/return the json string as JSONP

//=========================================================================================
?>