Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Compare 2 dates from Form with PHP

Posted on 2012-03-22
3
258 Views
Last Modified: 2012-04-29
Hi,

I do not have much experience coding and am trying to compare 2 dates as part of a form validation.

Basically, user inputs a "depart" date and a "return" date to a form.  The dates need to be compared, and if the return date is earlier than the depart date it should throw an error "Return date must be later than depart date" and stop the form from being sent with wrong dates.

Any help is appreciated!!

Thanks!
0
Comment
Question by:Whedonite
  • 2
3 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37755549
Read this while I put together an example that shows how this kind of testing is done.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Back in a moment... ~Ray
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37755569
See http://www.laprbass.com/RAY_temp_whedonite.php

Please post back if you have any questions, ~Ray
<?php // RAY_temp_whedonite.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');

// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_GET))
{
    $a = isset($_GET['a']) ? date('c', strtotime($_GET['a'])) : FALSE;
    $z = isset($_GET['z']) ? date('c', strtotime($_GET['z'])) : FALSE;

    // IF THE DATA IS BOGUS
    if (!$a) die('BOGUS DATA IN FIRST DATE');
    if (!$z) die('BOGUS DATA IN LAST DATE');

    // IF THE DEPART IS NOT EARLIER THAN THE RETURN
    if ($z <= $a) die('YOU CANNOT RETURN BEFORE YOU DEPART');

    // IF THE DATA IS OK
    echo PHP_EOL . 'YOU LEAVE ON ' . $a;
    echo PHP_EOL . 'YOU RETURN ON ' . $z;
}

// CREATE THE FORM WITH HEREDOC NOTATION
$form = <<<FORM
<form>
DEPART <input name="a" />
RETURN <input name="z" />
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37755988
I strongly suggest you also add a client validation since it is not worth going to the backend if the dates are wrong. That said, you always should test on the server too...
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
push Book Today button to right 10 34
PHP and JQuery Syntax question 4 26
Row insertion failed. Array 5 46
Test if unique ID is in log file 5 21
This article discusses how to create an extensible mechanism for linked drop downs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question