Cannot figure out the problem?

I am hoping someone can help me with this. I have racked my brain for the past 1 1/2 hours trying to solve this. First off, this is a school assignment so don’t think I am trying to lie to anyone. Anyway, this application should look like the first picture after two dates are entered. Instead, I am getting an error message from the catch function.

Even when I enter the date in the correct format (which is mm/dd/yyyy) it still doesn’t validate and move on to the next part of the code.

Any ideas on how to fix this?

Index

<?php
if (isset($_POST['action'])) {
    $action =  $_POST['action'];
} else {
    $action =  'start_app';
}

switch ($action) {
    case 'start_app':
   

        $message = 'Enter two dates and click on the Submit button.';
        break;
    case 'process_data':
        $invoice_date_s = $_POST['invoice_date'];
        $due_date_s = $_POST['due_date'];

        // make sure the user enters both dates
        if (empty($invoice_date_s) || empty($due_date_entry)) {
            $message = 'You must enter both dates. Please try again.';
            break;
        }

        // convert date strings to DateTime objects
        // and use a try/catch to make sure the dates are valid
        try {
            $invoice_date_o = new DateTime($invoice_date_s);
            $due_date_o = new DateTime($due_date_entry);
        } catch (Exception $e) {
            $message = 'Both dates must be in a valid format. Please check both dates and try again.';
            break;
        }

        // make sure the due date is after the invoice date
        if ($due_date_o < $invoice_date_o) {
            $message = 'The due date must come after the invoice date. Please try again.';
            break;
        }

        // set a format string for all dates
        $format_string = 'F j, Y';

        // format both dates
        $invoice_date_f = $invoice_date_o->format($format_string);
        $due_date_f = $due_date_o->format($format_string);

        // get the current date and time and format it
        $current_date_o = new DateTime();
        $current_date_f = $current_date_o->format($format_string);
        $current_time_f = $current_date_o->format('g:i:s a');

        // get the amount of time between the current date and the due date
        $time_span = $current_date_o->diff($due_date_o);
        if ($due_date_o < $current_date_o) {
            $due_date_message = $time_span->format(
                'This invoice is %y years, %m months, and %d days overdue.');
        } else {
            $due_date_message = $time_span->format(
                'This invoice is due in %y years, %m months, and %d days.');
        }


        break;
}
include 'date_tester.php';
?>

date_tester.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Date Tester</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
    <div id="content">
        <h1>Date Tester</h1>
        <form action="." method="post">
        <input type="hidden" name="action" value="process_data"/>

        <label>Invoice Date:</label>
        <input type="text" name="invoice_date"
               value="<?php echo htmlspecialchars($invoice_date_s); ?>"/>
        <br />

        <label>Due Date:</label>
        <input type="text" name="due_date"
               value="<?php echo htmlspecialchars($due_date_entry); ?>"/>
        <br />

        <label>&nbsp;</label>
        <input type="submit" value="Submit" />
        <br />

        </form>
        <h2>Message:</h2>
        <?php if ($message != '') : ?>
            
            <p><?php echo $message; ?></p>
        <?php else : ?>
        <table cellspacing="5px">
            <tr>
                <td>Invoice date:</td>
                <td><?php echo $invoice_date_f; ?></td>
            </tr>
            <tr>
                <td>Due date:</td>
                <td><?php echo $due_date_f; ?></td>
            </tr>
            <tr>
                <td>Current date:</td>
                <td><?php echo $current_date_f; ?></td>
            </tr>
            <tr>
                <td>Current time:</td>
                <td><?php echo $current_time_f; ?></td>
            </tr>
            <tr>
                <td>Due date message:</td>
                <td><?php echo $due_date_message; ?></td>
            </tr>
        </table>
        <?php endif; ?>

    </div>
</body>
</html>

This will get you closer.

Change:

[php]$invoice_date_o = new DateTime($invoice_date_s);
due_date_o = new DateTime($due_date_entry);[/php]

To:

[php]$invoice_date_o = DateTime::createFromFormat(‘d/m/Y’,$invoice_date_s)->format(‘Y-m-d’);
$due_date_o o = DateTime::createFromFormat(‘d/m/Y’,$due_date_entry)->format(‘Y-m-d’);
[/php]

But it will still allow some invalid dates like ‘04/44/2013’ but it will stop ‘01/aa/2111’

So before you convert it to a DateTime object you should check it with a preg_match function

preg_match("/([012]?[1-9]|[12]0|3[01])/(0?[1-9]|1[012])/([0-9]{4})/", ,$invoice_date_s)

Which will give you something like this…

[php]
if (preg_match("/([012]?[1-9]|[12]0|3[01])/(0?[1-9]|1[012])/([0-9]{4})/", ,$invoice_date_s))
{
$invoice_date_o = DateTime::createFromFormat(‘d/m/Y’,$invoice_date_s)->format(‘Y-m-d’);
}
else
{
$message = ‘Both dates must be in a valid format. Please check both dates and try again.’;
}
[/php]

Good Luck on your Assignment.

You are expecting data which may not be present
[php]case ‘process_data’:
$invoice_date_s = $_POST[‘invoice_date’];
$due_date_s = $_POST[‘due_date’];[/php]

These should be changed so you have a fall-back default
[php]$invoice_date_s = !empty($_POST[‘invoice_date’]) ? $_POST[‘invoice_date’] : ‘’;
$due_date_s = !empty($_POST[‘due_date’]) ? $_POST[‘due_date’] : ‘’;[/php]

Here you are also expecting $message to exist
[php]<?php if ($message != '') : ?>[/php]

And please use braces…
[php]<?php if (!empty($message)) { ?>[/php]

[hr]

You are mixing up variable names

[php]// make sure the user enters both dates
if (empty($invoice_date_s) || empty($due_date_entry)) {[/php]

$due_date_entry is not set, you do this several places and all will have to be changed to $due_date_s

[hr]

My suggestion:

[php] <?php
date_default_timezone_set(‘Europe/Oslo’); // List of supported time zones: http://www.php.net/manual/en/timezones.php

if (isset($_POST[‘action’])) {
$action = $_POST[‘action’];
} else {
$action = ‘start_app’;
}

/*

  • I would do it like this: it would give you default dates that
    1. show the user the correct formatting
    1. show that you can select todays date and + 14 days.
  • $invoice_date_s = !empty($_POST[‘invoice_date’]) ? $_POST[‘invoice_date’] : date(‘m/d/Y’);
  • $due_date_s = !empty($_POST[‘due_date’]) ? $_POST[‘due_date’] : date(‘m/d/Y’, strtotime(’+14 day’));
    */
    $invoice_date_s = !empty($_POST[‘invoice_date’]) ? $_POST[‘invoice_date’] : ‘’;
    $due_date_s = !empty($_POST[‘due_date’]) ? $_POST[‘due_date’] : ‘’;

switch ($action) {
case ‘start_app’:

     $message = 'Enter two dates and click on the Submit button.';
     break;
 case 'process_data':

     // make sure the user enters both dates
     if (empty($invoice_date_s) || empty($due_date_s)) {
         $message = 'You must enter both dates. Please try again.';
         break;
     }

     // convert date strings to DateTime objects
     // and use a try/catch to make sure the dates are valid
     try {
         $invoice_date_o = new DateTime($invoice_date_s);
         $due_date_o = new DateTime($due_date_s);
     } catch (Exception $e) {
         $message = 'Both dates must be in a valid format. Please check both dates and try again.';
         break;
     }

     // make sure the due date is after the invoice date
     if ($due_date_o < $invoice_date_o) {
         $message = 'The due date must come after the invoice date. Please try again.';
         break;
     }

     // set a format string for all dates
     $format_string = 'F j, Y';

     // format both dates
     $invoice_date_f = $invoice_date_o->format($format_string);
     $due_date_f = $due_date_o->format($format_string);

     // get the current date and time and format it
     $current_date_o = new DateTime();
     $current_date_f = $current_date_o->format($format_string);
     $current_time_f = $current_date_o->format('g:i:s a');

     // get the amount of time between the current date and the due date
     $time_span = $current_date_o->diff($due_date_o);
     if ($due_date_o < $current_date_o) {
         $due_date_message = $time_span->format(
             'This invoice is %y years, %m months, and %d days overdue.');
     } else {
         $due_date_message = $time_span->format(
             'This invoice is due in %y years, %m months, and %d days.');
     }


     break;

}
?>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Date Tester

Date Tester

     <label>Invoice Date:</label>
     <input type="text" name="invoice_date"
            value="<?php echo htmlspecialchars($invoice_date_s); ?>"/>
     <br />

     <label>Due Date:</label>
     <input type="text" name="due_date"
            value="<?php echo htmlspecialchars($due_date_s); ?>"/>
     <br />

     <label>&nbsp;</label>
     <input type="submit" value="Submit" />
     <br />

     </form>
     <h2>Message:</h2>
     <?php if (!empty($message)) { ?>
        
         <p><?php echo $message; ?></p>
     <?php } else { ?>
     <table cellspacing="5px">
         <tr>
             <td>Invoice date:</td>
             <td><?php echo $invoice_date_f; ?></td>
         </tr>
         <tr>
             <td>Due date:</td>
             <td><?php echo $due_date_f; ?></td>
         </tr>
         <tr>
             <td>Current date:</td>
             <td><?php echo $current_date_f; ?></td>
         </tr>
         <tr>
             <td>Current time:</td>
             <td><?php echo $current_time_f; ?></td>
         </tr>
         <tr>
             <td>Due date message:</td>
             <td><?php echo $due_date_message; ?></td>
         </tr>
     </table>
     <?php } ?>

 </div>
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service