Rabin Lama Rabin Lama - 19 days ago 4
PHP Question

how to know if a date/time falls in some date/time range?

For example I have a date/time range like this

<?php
$dateStart = '11-28 00:00:00';
$dateEnd = '11-28 23:59:59';
?>


Now I want to get the current date and time of the user and check if it falls inside the given date range or not. How do I do this ?

Answer

You can use this logic:

$start_date = '2012-07-10';
$end_date = '2002-10-06';
$date_from_user = '2009-08-28';

checkInRange($start_date, $end_date, $date_from_user);

function checkInRange($start_date, $end_date, $date_from_user)
{
  // Convert to timestamp
  $start_timestamp = strtotime($start_date);
  $end_timestamp = strtotime($end_date);
  $user_timestamp = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($user_timestamp >= $start_timestamp) && ($user_timestamp <= $end_timestamp));
}

Hope this helps!

Comments