index
<?php
//set default value
$message = '';
//get value from POST array
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = 'start_app';
}
//process
switch ($action) {
case 'start_app':
// set default invoice date 1 month prior to current date
$interval = new DateInterval('P1M');
$default_date = new DateTime();
$default_date->sub($interval);
$invoice_date_s = $default_date->format('n/j/Y');
// set default due date 2 months after current date
$interval = new DateInterval('P2M');
$default_date = new DateTime();
$default_date->add($interval);
$due_date_s = $default_date->format('n/j/Y');
$message = 'Enter two dates and click on the Submit button.';
break;
case 'process_data':
$invoice_date_s = filter_input(INPUT_POST, 'invoice_date');
$due_date_s = filter_input(INPUT_POST, 'due_date');
// make sure the user enters both dates
if (empty($invoice_date_s) || empty($due_date_s)) {
$message = 'Please insert both dates';
break;
}
// and use a try/catch to make sure the dates are valid
try {// convert date strings to DateTime objects
$invoice_date_object = new DateTime($invoice_date_s);
$due_date_object = new DateTime($due_date_s);
} catch (Exception $e) {
$message = 'Please make sure dates are in a valid format';
break;
}
// make sure the due date is after the invoice date
if ($due_date_object < $invoice_date_object ){
$message = 'The Due Date can not be before the Invoice Date please make sure
the correct dates are input';
break;
}
// format both dates
$invoice_date_f = $invoice_date_object->format('F d,Y');
$due_date_f = $due_date_object->format('F d,Y');
// get the current date and time and format it
$current_date_object = new DateTime();
$current_date_f = $current_date_object->format('F d,Y');
$current_time_f = $current_date_object->format('g:i:s a');
// get the amount of time between the current date and the due date
$time_span = $current_date_object->diff($due_date_object);
// and format the due date message
if ($due_date_object > $current_date_object){
$due_date_message = $time_span-> format(' This invoice is due in %y years, %m months, and %d days');
}else {
$due_date_message = $time_span-> format('This invoice is %y years, %m months, and %d days overdue');
}
break;
};
include 'date_tester.php';
?>