Jump to content


Photo

Recognizing date as invalid


  • Please log in to reply
4 replies to this topic

#1 gravsdsnc

gravsdsnc

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 18 October 2013 - 07:07 PM

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>&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>


#2 requinix

requinix

    Playful Member

  • Moderators
  • 5,744 posts
  • LocationWA

Posted 18 October 2013 - 07:18 PM

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.

#3 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 13,885 posts
  • LocationCheshire, UK

Posted 18 October 2013 - 07:32 PM

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/m...efromformat.php


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#4 requinix

requinix

    Playful Member

  • Moderators
  • 5,744 posts
  • LocationWA

Posted 18 October 2013 - 08:04 PM

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/m...efromformat.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.



#5 gravsdsnc

gravsdsnc

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 18 October 2013 - 09:55 PM

Sleepy Member...

 

Thanks a million!  The date_default_timezone_set was the issue.  I guess it didn't know where to start!  Thanks so much.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com