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
https://forums.phpfreaks.com/topic/28824-compare-date/
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
https://forums.phpfreaks.com/topic/28824-compare-date/#findComment-131965
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.