Solved

Compare 2 dates from Form with PHP

Posted on 2012-03-22
3
260 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
[X]
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
  • 2
3 Comments
 
LVL 110

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 110

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

691 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