Solved

Error checking dates

Posted on 2011-03-13
8
272 Views
Last Modified: 2012-05-11
Hi Experts, please see my attached code. I am new to PHP and this is my first attempt. I have got error checking in place. But I am trying to test that the departure date is greater than the arrival date and I cant seem to implement it into my existing code.

My IF statement looks like this...

if ( $departure_date <= $arrival_date ) {
echo "oops error";
} else {
echo "All good";
}

Thanks,
Dean
<?
$dbhost = 'X';

$dbuser = 'X';
$dbpass = 'X';
$dbname = 'X';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

 

if(!empty($_POST) && isset($_POST))

{

    $emptyvariables = false;

    if(!empty($_POST)){
        foreach($_POST as $key => $val){
            if(empty($_POST[$key])){
                echo"Please insert $key<br />";
                $emptyvariables = true;

                }
        }
    }

    if($emptyvariables == false){

        $id = mysql_real_escape_string($_POST['id']);
        $Forename = mysql_real_escape_string($_POST['Forename']);
        $Surname = mysql_real_escape_string($_POST['Surname']);
        $Arrival_Date = mysql_real_escape_string($_POST['Arrival_Date']);
        $Departure_Date = mysql_real_escape_string($_POST['Departure_Date']);
        $RoomNo = mysql_real_escape_string($_POST['RoomNo']);
        $Tel = mysql_real_escape_string($_POST['Tel']);

				$Arrival_Date = str_replace("/","-",($Arrival_Date));
        $Arrival_Date = date("Y-m-d", strtotime($Arrival_Date));

				$Departure_Date = str_replace("/","-",($Departure_Date));
        $Departure_Date = date("Y-m-d", strtotime($Departure_Date));

     
           $sql = "
            INSERT INTO bookings
            SET
            id = '$id',
            Forename = '$Forename',
            Surname = '$Surname',
            Arrival_Date = '$Arrival_Date',
            Departure_Date = '$Departure_Date',
            RoomNo = '$RoomNo',
            Tel = '$Tel'";          

            echo"$query";

                                                                        
        $result = mysql_query($sql);

        if(!$result)
        {
            echo "Booking already exists.";
        }
        else
        {
            echo "Record inserted successfully";
        }
    }
}

?>

<table width="300" border="1" cellpadding='5'cellspacing='5'>
   <tr align="center">
     <td><b>ID</b></td>
     <td><b>Forename</b></td>
     <td><b>Surname</b></td>
     <td><b>Arrival_Date</b></td>
     <td><b>Departure_Date</b></td>
     <td><b>RoomNo</b></td>
     <td><b>Tel</b></td>
   </tr>

<?

$sql = "
SELECT *
FROM bookings
ORDER BY ID ASC
LIMIT 20";

 $result = mysql_query($sql);
 while($row = mysql_fetch_array($result))

   {
?>


<?
$arrival_date_formatted = date("d-m-Y", strtotime($row['Arrival_Date']));
$departure_date_formatted = date("d-m-Y", strtotime($row['Departure_Date']));
?>

 <tr>
   <td><? echo $row['id']; ?></td>
   <td><? echo $row['Forename']; ?></td>
   <td><? echo $row['Surname']; ?></td>
   <td><? echo $arrival_date_formatted; ?></td>
   <td><? echo $departure_date_formatted; ?></td>
   <td><? echo $row['RoomNo']; ?></td>
   <td><? echo $row['Tel']; ?></td>
  </tr>

<?

  }

?>          

</table>

Open in new window

0
Comment
Question by:deanlee17
  • 4
  • 4
8 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35121643
When checking dates ALWAYS make sure they are in YYYY-MM-DD format and then tests like "greater than" or "less than" will always work correctly.

Some other points - don't use <? as opening tags as not all PHP setups will process them. They are an old style and these days they confuse the PHP parse if the document contains XML which opens with ..... <? tags.  Always use <?php as the opening tag.

 
0
 

Author Comment

by:deanlee17
ID: 35121678
Thanks for the hints. But I need help implementing it into my code.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35121711
What format are the dates being entered in? What I mean is what is the date format of $_POST['Arrival_Date'] before you do anything to it?

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:deanlee17
ID: 35121746
User enters in this format DD-MM-YYYY

It is then formtted into YYYY-MM-DD just before the table insert (lines 40 & 43)

Thanks.

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35121762
OK, then after this point

        $Arrival_Date = str_replace("/","-",($Arrival_Date));
        $Arrival_Date = date("Y-m-d", strtotime($Arrival_Date));

        $Departure_Date = str_replace("/","-",($Departure_Date));
        $Departure_Date = date("Y-m-d", strtotime($Departure_Date));

the dates will be in YYYY-MM-DD format which means you can directly compare them


if ( $Departure_Date <= $Arrival_Date ) {
    echo "oops error";
} else {
    echo "All good";
}

Note that I have capitalised the names as PHP is case sensitive. Are we any further forward? I know you intend to output more than "oops" but what?
0
 

Author Comment

by:deanlee17
ID: 35121780
Ok thats all well and good, but I cant implement the IF statement so that it echo's the error else continues on and does the insert. Usually it throws the error then goes on to post anyway lol.

I usually have.....

if ( $Departure_Date <= $Arrival_Date ) {
    echo "oops error";
} else {


Then close the end bracket around line 70

Thanks
0
 

Author Comment

by:deanlee17
ID: 35121878
Aha! got it working, ur first post about which format to use (YYYY-MM-DD) helped the most so I will award points to that post.

Many Thanks
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35121905
Glad you're sorted.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decrypt string by php 7 48
mysqli_connect error on wamp but not on remote server 8 38
How to obtain the string from a PHP StdObject ? 6 33
JQuery Search Filter 2 39
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

830 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