0

For example I have a date/time range which doesn't have year, and which needs to be converted to timestamp like below,

<?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 ?

Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33

1 Answers1

3

You can use this logic:

$start_date = '2012-07-10';
$end_date = '2012-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!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
  • Will this `strtotime()` method work even after I remove the year from the given date ? And what about the time ? Thank you for your time. – Rabin Lama Dong Nov 13 '16 at 16:28
  • In case of time, just append the start_time, end_time, etc with the time string like this - `2012-07-10 00:00:00` – Saumya Rastogi Nov 13 '16 at 16:29
  • Okay. and how do I get the current date and time of the user through php ? Sorry for the trouble. For example, get the date and time of the user who is using the website from Japan or USA or any other place ? – Rabin Lama Dong Nov 13 '16 at 16:37