Jump to content

Recommended Posts

Hello all,

   I am working on a school assignment and cannot seem to figure out the issue.  I have been trying to fix this for over 2 hours.  I am hoping somone can help.  I have a date-checker application that is not validating properly.  

 

It is supposed to do this

Date-Tester.png

 

Instead it is doing this

error-message.png

 

Code is below

Index.php

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

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 = $_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> </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>
Link to comment
https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/
Share on other sites

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;
}
An Exception is being thrown. What is it? If it has something to do with date.timezone then you need to set that value in your php.ini or call date_default_timezone_set in your code somewhere beforehand.

new DateTime($date) requires $date to be in yyyy-mm-dd format. To create a DateTime object from other formats use http://www.php.net/manual/en/datetime.createfromformat.php

Not really? The string is parsed the same way strtotime() does it.

time

A date/time string. Valid formats are explained in Date and Time Formats.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.