0

I am trying to check whether user submitted date is within range or not.

<?php
$datesyntx = "/^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$/";
$paydate = "2015-03-01";
if (preg_match($datesyntx, $paydate)) {
    if(strtotime($paydate) > strtotime('2015-04-23') && strtotime($paydate) < strtotime('2015-07-23')) {
 }
}
?>

It is not working what actually I am trying to get.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
  • 2
    Use PHP built-in class DateTime to compare dates, it's easier & you won't have to convert dates manually or run them through preg_match – ahmad Apr 18 '15 at 16:22
  • it's a duplicated question , yes was answered before : http://stackoverflow.com/questions/19070116/php-check-if-date-between-two-dates . – Root Apr 18 '15 at 16:29
  • I've answered it with easy way . test it . – Root Apr 18 '15 at 16:41

2 Answers2

0

Try this code :

$paydate   = date('2015-03-01');

$DateBegin = date('2010-01-01');
$DateEnd   = date('2016-01-01');

if (($paydate > $DateBegin) && ($paydate < $DateEnd))
{
  echo "is between";
}
else
{
  echo "not between!";  
}

P.S: this question was answered before at stackoverflow: PHP check if date between two dates EDIT2: easy way .

Community
  • 1
  • 1
Root
  • 2,269
  • 5
  • 29
  • 58
0

No need to use regex in this scenario, use simple DateTime class to check whether user submitted date is within range or not.you can also use typical strtotime() to do this job. But DateTime class will be great fun for this.

<?php
$userSubmitted = new DateTime("2015-04-01");
$startDate=new DateTime("2014-02-01 20:20:00");
$endDate=new DateTime("2015-04-30 23:50:11");
if ($userSubmitted > $startDate  && $userSubmitted < $endDate)
{
    return true;  #date is within the range of yours
}

return false;  #date not within the range of yours
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103