gravsdsnc Posted October 19, 2013 Share Posted October 19, 2013 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 Instead it is doing this 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> Quote Link to comment https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/ Share on other sites More sharing options...
requinix Posted October 19, 2013 Share Posted October 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/#findComment-1454497 Share on other sites More sharing options...
Barand Posted October 19, 2013 Share Posted October 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/#findComment-1454498 Share on other sites More sharing options...
requinix Posted October 19, 2013 Share Posted October 19, 2013 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.phpNot 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. Quote Link to comment https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/#findComment-1454499 Share on other sites More sharing options...
gravsdsnc Posted October 19, 2013 Author Share Posted October 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283091-recognizing-date-as-invalid/#findComment-1454508 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.