[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Compare 2 dates from Form with PHP

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

656 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