?
Solved

Compare 2 dates from Form with PHP

Posted on 2012-03-22
3
Medium Priority
?
261 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

752 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