Calculate OCXO Age
const PHP_API_ENDPOINT = 'https://www.g5sto.com/ocxo_age_calculator.php'; // Placeholder, user must change this!
function formatTime(totalSeconds) { if (isNaN(totalSeconds) || totalSeconds < 0) { return "Invalid date or calculation error."; } const years = Math.floor(totalSeconds / (365.25 * 24 * 3600)); // Use 365.25 for average year totalSeconds %= (365.25 * 24 * 3600); const days = Math.floor(totalSeconds / (24 * 3600)); totalSeconds %= (24 * 3600); const hours = Math.floor(totalSeconds / 3600); totalSeconds %= 3600; const minutes = Math.floor(totalSeconds / 60); const seconds = Math.floor(totalSeconds % 60); let result = ''; if (years > 0) result += `${years} years `; if (days > 0) result += `${days} days `; result += `${String(hours).padStart(2, '0')} hours ${String(minutes).padStart(2, '0')} minutes ${String(seconds).padStart(2, '0')} seconds`; return result.trim(); }
async function calculateAge() { const startDateInput = document.getElementById('startDate'); const resultDiv = document.getElementById('result'); const errorMessageDiv = document.getElementById('errorMessage');
errorMessageDiv.innerText = ''; // Clear previous errors resultDiv.innerText = 'Calculating...';
const startDate = startDateInput.value;
if (!startDate) { errorMessageDiv.innerText = 'Please select a start date.'; resultDiv.innerText = ''; return; }
try { // Send the start date to the PHP backend const response = await fetch(`${PHP_API_ENDPOINT}?start_date=${startDate}`);
if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }
const data = await response.json();
if (data.error) { errorMessageDiv.innerText = `Error: ${data.error}`; resultDiv.innerText = ''; } else if (typeof data.total_seconds === 'number') { resultDiv.innerText = formatTime(data.total_seconds); } else { errorMessageDiv.innerText = 'Unexpected response from server.'; resultDiv.innerText = ''; }
} catch (error) {
console.error('Fetch error:', error);
errorMessageDiv.innerText = 'Could not connect to the calculation service. Please check the PHP_API_ENDPOINT URL.';
resultDiv.innerText = '';
}
}