php date between range code example

Example: check if date is within range php

$start_date = '2020-10-21';
$end_date = '2020-10-15';
$date_check = '2020-08-28';

if ($this->check_in_range($start_date, $end_date, $date_from_user)) {
  echo "within range";
} else {
  echo "not within range";
}

function check_in_range($start_date, $end_date, $date_from_user) {
  // Convert to timestamp
  $start = strtotime($start_date);
  $end = strtotime($end_date);
  $check = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($start <= $check ) && ($check <= $end));
}

Tags:

Php Example