-3

I have an app which should check that if Time is not within the required range then person should not able buy anything.

I am using PHP functions as below to check current time:

date_default_timezone_set ("Asia/Calcutta");  
$date =  date("H:i", time()); 

The time returned is correct but how can i check if time is in between 11:00AM to 11:00PM

  • 1
    It wasn't me so I'm just guessing: probably someone thought you did too little research. A short check on SO turns out http://stackoverflow.com/questions/4191867/php-function-to-check-time-between-the-given-range , which you may want to check out. – LSerni Aug 29 '14 at 20:57

1 Answers1

3

I like using DateTime() for this as it is very readable (and deals with any potential datetime issues like daylight savings time but that doesn't apply here):

$datetime = new DateTime(null, new DateTimeZone("Asia/Calcutta"));
$am       = new DateTime('11am');
$pm       = new DateTime('11pm');
if ($datetime > $am && $datetime < $pm) {
    // between 11am and 11pm
}

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496