<?php
$json_url = "http://gsx2json.com/api?id=1MxnHv59agY8gElOnaTfC34LfdIV85OwwE6CQsZXqmrA&sheet=1";
$json = file_get_contents($json_url);
#decode gets the json file and turns it into a php array
$data = json_decode($json, TRUE);
#json gives data in rows and in colums, you want it in rows
$json = $data['rows'];
print_r($data);
echo gettype(date("m/d/y"));

if (file_exists('calendar.json')){
	$serverjson = fopen('calendar.json', "r");
	$serverjson = fread($serverjson,filesize("calendar.json"));
	$serverjson = json_decode($serverjson,TRUE);
	//if the jsons are not the same
	if($serverjson!=$json){
		$serverjson = $json;
		$serverjson = json_encode($serverjson,TRUE);
		//puts the new updated json in the server
		unlink('calendar.json');
		$filename = 'calendar.json';
		$newfile = fopen($filename,'w');
		fwrite($newfile,$serverjson);
		fclose($newfile);
		//Puts the day it was updated, month/day/year
		$filename = ('updatedcalendar.txt');
		$newfile = fopen($filename,'w');
		$date = date("m/d/y");
		$time = date("h:i:sa"); 
		fwrite($newfile,"$date $time");
		fclose($newfile);
	}
}

	#Two cases, in case the modified bit is 1 or 0 (will be diferent from the other one because it was already made with modified bit 1)
	#If it had modified bit 0 then try to chage its modified bit temporarily to compare
	// if ($serverjson['modified'] == 1){
	// 	$serverjson = json_encode($serverjson);
	// 	#if the jsons are not the same then it replaces the file
	// 	if ($serverjson != $json){
	// 		unlink('calendar.json');
	// 		#Make a new json with what you have and make a new file that is called calendar.json
	// 		$filename = 'calendar.json';
	// 		$newfile = fopen($filename,'w');
	// 		fwrite($newfile,$json);
	// 		fclose($newfile);
	// 	} 

	// }
// 	else{
// 		$serverjson['modified'] = 1;
// 		$serverjson = json_encode($serverjson);
// 		if ($serverjson != $json){
// 			unlink('calendar.json');
// 			#Make a new json with what you have and make a new file that is called calendar.json
// 			$filename = 'calendar.json';
// 			$newfile = fopen($filename,'w');
// 			fwrite($newfile,$json);
// 			fclose($newfile);
// 		}
// 	}

// 	#if the jsons are not the same then it replaces the file
// 	if ($serverjson != $json){
// 		unlink('calendar.json');
// 		#Make a new json with what you have and make a new file that is called calendar.json
// 		$filename = 'calendar.json';
// 		$newfile = fopen($filename,'w');
// 		fwrite($newfile,$json);
// 		fclose($newfile);
// 	} 

// }
#--------------------------------------------------------------
#print_r($data);
#counts the amount of events in the google sheets that Ill use to compare with the ones in the server
#echo count($data);
#first one of the festival names
#echo ($data[0]['nombredefestival']);
#This to access the modified bit
#echo ($data['modified'])
#--------------------------------------------------------------
else{
	$filename = 'calendar.json';
	$newfile = fopen($filename,'w');
	fwrite($newfile,$json);
	fclose($newfile);
	//Puts the day it was updated, month/day/year
	$filename = ('updatedcalendar.txt');
	$newfile = fopen($filename,'w');
	$date = date("m/d/y");
	$time = date("h:i:sa"); 
	fwrite($newfile,"$date $time");
	fclose($newfile);
	}

?>