Jump to content

Validating Appointment Dates


elmas156

Recommended Posts

Hello, I'm having a problem validating dates of an appointment system that I'm creating.  Basically what I've got is a calendar/date selector that allows a user to select a date in the format "09/07/2008" and schedule an appointment.  I know that I can get the current date in the same format using php by doing this:

 

$cmonth=date("m");

$cday=date("d");

$cyear=date("Y");

$cdate=$cmonth  ."/".  $cday  ."/".  $cyear;

 

What I'm trying to do is run a query to get the scheduled appointments and compare the appointment dates with the current date to see if the appointment dates have already passed.  If there is an appointment scheduled and it is in the future I want the page to notify the user, if the appointment date has already passed it should allow the user to schedule an appointment.  I got this to work using the method below but the problem that I run into is if the current date is 09/08/2008 and the appointment date is any date after the last day of this year, the system is recognizing that the appointment date has already passed, therefore not notifying the user.  Any ideas on how to solve this problem?

 

Here's what I have now:

<?php

$cmonth=date("m");
$cday=date("d");
$cyear=date("Y");
$cdate=$cmonth  ."/".  $cday  ."/".  $cyear;

$scheduled = mysql_query("SELECT apptdate FROM appts WHERE email = '$email' AND pw = '$pw'");
$d = mysql_num_rows($scheduled); // Checks to see if anything is in the db.

$row = mysql_fetch_array($scheduled);

$apptdate = $row[ "apptdate" ];

if ( ($d) && ($apptdate > $cdate) ) {

echo "<font color=\"#FF0000\">*Scheduled for $apptdate*</font>";
echo " <input name=\"schedule\" type=\"submit\" value=\"View Appointment Information\">";

} elseif (!$d || $apptdate < $cdate) {

echo " <input name=\"schedule\" type=\"submit\" value=\"Schedule My Appointment.....\">";

} 

?>

Link to comment
Share on other sites

If you want to compare dates, constructing them like this works well:

 

$cdate = "$cyear-$cmonth-$cday";

 

You can keep two representations of the date - one to use for internal calculations (in the order year-month-day) and another to display to the user (in the order month-day-year).

Link to comment
Share on other sites

I think I understand but I'm not totally sure... basically, I need to change the format of the date to be "2008/09/08" to compare them the way I need to?  That would bring up another problem... I'm not sure how to change the date format using the calendar/date selector that I'm using, much less having two different formats of the same date.  Is there a way that I can take a date and break it down and make variables out of part of it and not the rest?  For example:

 

take the date 09/15/2008 and make

 

$amonth = "09";

$aday = "15";

$ayear = "2008";

 

If there was a way to do this I think I could put something together that would work well.  Any ideas?  Thanks.

Link to comment
Share on other sites

You could explode by the the slash:

 

$date = "09/15/2008";
list($amonth,$aday,$ayear) = explode('/',$date);

 

If you're selecting from MySQL, you could also use the function DATE_FORMAT. See here for more on that.

 

It's a new one for me that you can compare dates by having them in that order; i would have suggested converting to a unix timestamp.

Link to comment
Share on other sites

Well, a unix timestamp is the number of seconds since the unix epoch - 1st January 1970. Since this is an integer, you can compare dates easily. The php function time() returns a unix timestamp, and you can convert other dates either with mktime() or strtotime(). You can convert MySQL dates with the UNIX_TIMESTAMP() function.

Link to comment
Share on other sites

Ok, another question... when a user selects their appointment date and schedules their appointment using the date format "09/25/2008" and it is inserted into a MySQL database and if the appttime field type is set to date, what is actually inserted into the database is 0000-00-00.  Why is this and what can I do to fix it to actually show it in the format that I need?

Link to comment
Share on other sites

Ginger, the comparison works in that order because it's a character-by-character string comparison.  Eg

 

"2008-10-21" > "2008-05-10"

 

PHP will compare each digit of 2008 in turn, finding all are the same.  Then it will compare "-", finding it the same.  Then it compares 1 with 0 (the first digit of the month), and determines that 1 is greater.  Therefore "2008-10-21" > "2008-05-10".

 

Elmas, you could make a function like this:

 

function compare_dates($a, $b) {
  list($amonth, $aday, $ayear) = explode('/', $a);
  list($bmonth, $bday, $byear) = explode('/', $b);

  $a2 = "$ayear-$amonth-$aday";
  $b2 = "$byear-$bmonth-$bday";
  if ($a2 < $b2) return -1;
  if ($a2 > $b2) return 1;
  return 0;
}

$d1 = '09/25/2008';
$d2 = '09/26/2007';
if (compare_dates($d1, $d2) == 1) {
  print "$d2 comes before $d1\n";
}

 

That allows you to keep your date in mdy format in your program, but still compare them correctly.

 

Edit: compare_dates() returns 3 possible values, -1 means "d1 < d2", 0 means "d1 == d2", 1 means "d1 > d2"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.