PRISON OF MIRRORS We're not here because we're free. We're here because we're not free.
Enter the birthdate of Person 1
(the younger person)
Date:
Enter the birthdate of Person 2
(the older person)
Date:
On the date above, Person 1 will be half Person 2's age. On the date above, Person 2 will be twice Person 1's age.

Project

This project takes two birthdates, and then calculates when the older birthdate is twice as old as the younger one.

Source Code

Here's the source code of the project, namely the code that does the validation and the code that actually calculates the date.

$(document).ready(function()
{
	//Sets up the two datepickers
	$('#younger, #older').datepicker(
	{
		mindate: new Date(1900, 1, 1),
		maxdate: '+1y',
		changeMonth: true,
		changeYear: true,
		showAnim: 'slideDown',
		yearRange: '1950:2012',
		dateFormat: 'mm-dd-yy'
	});
});

/**
 * Calculates the date.
 */
function get_the_date()
{
	var younger = $.trim($('#younger').val());
	var older = $.trim($('#older').val());
	
	//Make sure we have values to work with
	if(younger.length < 1)
	{
		alert('Please enter a birthdate for Person 1.');
		return;
	}
	
	if(older.length < 1)
	{
		alert('Please enter a birthdate for Person 2.');
		return;
	}
	
	$.get('http://www.prisonofmirrors.com/wp-content/themes/prison/projects/find-your-half/ajax/find-your-half.php?younger=' + younger + '&older=' + older, function(data)
	{
		if(data.response)
		{
			$('div.result').empty().append(data.result);
			$('#response').empty();
		}
		else
		{
			$('div.result').empty();
			$('#response').html(data.message);
		}
	}, 'json');
}
            
if(isset($_GET['younger']) && isset($_GET['older']))
{
	//Ensure we're working with valid dates
	$younger_arr = explode('-', $_GET['younger']);
	$older_arr = explode('-', $_GET['older']);
	
	if(!is_array($younger_arr) || !is_array($older_arr))
	{
		$array = array('response' => false, 
			       'message' => '<div class="error">Please enter a properly formatted date.</div>');
		echo json_encode($array);
		exit;
	}
	
	if(!checkdate($younger_arr[0], $younger_arr[1], $younger_arr[2]))
	{
		$array = array('response' => false, 
			       'message' => '<div class="error">Please enter a correct date for Person 1.</div>');
		echo json_encode($array);
		exit;
	}
	
	if(!checkdate($older_arr[0], $older_arr[1], $older_arr[2]))
	{
		$array = array('response' => false, 
			       'message' => '<div class="error">Please enter a correct date for Person 2.</div>');
		echo json_encode($array);
		exit;
	}
	
	$older = new DateTime($older_arr[2].'-'.$older_arr[0].'-'.$older_arr[1]);
	$younger = new DateTime($younger_arr[2].'-'.$younger_arr[0].'-'.$younger_arr[1]);
	
	//Ensure Person 1 is younger than Person 2
	if($older >= $younger)
	{
		$array = array('response' => false, 
			       'message' => '<div class="error">Person 2 must be older than Person 1.</div>')
		echo json_encode($array);
		exit;
	}
	
	//Calculate how old in days Person 2 is when Person 1 is born
	$days = floor((strtotime($younger_arr[2].'-'.$younger_arr[0].'-'.$younger_arr[1]) - strtotime($older_arr[2].'-'.$older_arr[0].'-'.$older_arr[1])) / (60 * 60 * 24));
	
	//Add the days to Person 1's date of birth for the final date
	$younger->modify('+'.$days.' days');
	
	echo json_encode(array('response' => true, 'result' => $younger->format('F j, Y')));
}
else
{
	$array = array('response' => false,
		       'message' => '<div class="error">Sorry, there was an error getting the date. Please try again.</div>');
	echo json_encode($array);
}