Jump to content

Compare date


austine_power007

Recommended Posts

Hi all

I am using PHP and Microsoft access 2003 for a project....now i want to compare two date to be sure user didn't give wrong start and end date...
Like:

Start date :
End date:

The form takes data from the two filed and then compare that end date is not before the start date also it checks that is it in right format: like: mm/dd/yy

I am new so i am confused how to do that  ???

Thanks in advance.  ;D
Link to comment
Share on other sites

For dates in y-m-d format, you can just use [code=php:0]if ($start_date > $end_date) { ... }[/code]

For your case, it's a bit messier.  I think the easiest is to extract the year, date and month and compare each one.

[code=php:0]if (preg_match('|[[:digit:]]*/[[:digit:]]*/[[:digit:]]*|', $start_date, &$matches)) {
  $start_m = $matches[1];
  $start_d = $matches[2];
  $start_y = $matches[3];
} else {
  echo "Invalid start date\n";
}
if (preg_match('|[[:digit:]]*/[[:digit:]]*/[[:digit:]]*|', $end_date, &$matches)) {
  $end_m = $matches[1];
  $end_d = $matches[2];
  $end_y = $matches[3];
} else {
  echo "Invalid end date\n";
}
$start_cmp = "$start_y-$start_m-$start_d";
$end_cmp = "$end_y-$end_m-$end_d";
if ($start_cmp > $end_cmp) {
  echo "Start date is after end date.  Cannot lar!\n";
}[/code]
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.