Solved

Compare 2 dates from Form with PHP

Posted on 2012-03-22
3
245 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 108

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 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now