83

I have following three strings (current time, sunset and sunrise), how can I find if the current time is between the sunrise and sunset?

$current_time = "10:59 pm";
$sunrise = "5:42 am";
$sunset = "6:26 pm";
JJJ
  • 32,902
  • 20
  • 89
  • 102
user966582
  • 3,225
  • 4
  • 32
  • 33
  • Take a look at the DateTime Class if the above doesnt help you, OP. – phpisuber01 Apr 09 '13 at 19:57
  • 20
    I don't think it's fair to downvote this question. The linked other answer strictly deals with dates. I think people just 'go with the flow' as soon as any question hits -1. – Evert Apr 09 '13 at 19:58
  • 1
    @WebnetMobile i think comparing dates are a bit diferent then comparing daytimes. – Ace Apr 09 '13 at 19:58
  • 3
    wow? why I got the down votes? Isn't this site suppose to ask questions if you can't do something on your own? – user966582 Apr 09 '13 at 19:59
  • @JohnConde awesome. This is a basic question which I could not do on my own. I could convert to minutes if it were 24 hours format, but because of AM/PM, I couldnt get it to work so I asked question. – user966582 Apr 09 '13 at 20:00
  • This is really comparing dates, because you imply that all three times are on the same date. Using @John Conde's method converts all three time strings to date objects "TODAY" - so today at 5:42am, etc. – James Apr 09 '13 at 20:07
  • Always show what you've tried. We know it doesn't work. That's why you;re asking for help. But by showing us you tried we don't feel like we're just doing your work for you. – John Conde Apr 09 '13 at 20:07

2 Answers2

121
$current_time = "4:59 pm";
$sunrise = "5:42 am";
$sunset = "6:26 pm";
$date1 = DateTime::createFromFormat('h:i a', $current_time);
$date2 = DateTime::createFromFormat('h:i a', $sunrise);
$date3 = DateTime::createFromFormat('h:i a', $sunset);
if ($date1 > $date2 && $date1 < $date3)
{
   echo 'here';
}

See it in action

Reference

No Sssweat
  • 358
  • 1
  • 6
  • 24
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • notice that he's not dealing with Date values, are you shure that this is working with daytime only ?, i think that DateTime function is dealing with a unix timestamp in the core function. if date3 is = day 15 - 6 pm , this would be false = day 17 - 4 pm < date3 – Ace Apr 09 '13 at 20:07
  • please corect me if i'm wrong, i didnt try out the function – Ace Apr 09 '13 at 20:10
  • Check out the fiddle i set up. It shows it working. – John Conde Apr 09 '13 at 20:13
  • alright, i did not know that the funktion works correctly with only time values ^^ – Ace Apr 09 '13 at 20:14
  • Thanks for the answer. Is it possible to do this without `DateTime::createFromFormat` ? I have been trying to search some alternative function to convert the date but could not find. – user966582 Apr 09 '13 at 20:32
  • Is there any reason why you *don't* want to use that function? – John Conde Apr 09 '13 at 20:34
  • Because its not supported in older versions of PHP. – user966582 Apr 09 '13 at 20:49
  • 5.3 is the oldest supported version of PHP. So if someone is using and older version they really should be upgrading. – John Conde Apr 09 '13 at 20:50
  • An oldie but a goodie ;) – Funk Forty Niner Sep 13 '14 at 00:17
  • 11
    This may work in the simple case that all time periods are on the same day but if you're checking if 10pm is greater than 8pm and less than 2am, this will fail. – oucil Oct 22 '14 at 19:18
  • 3
    Small edit(if current time is not given manually). `$current_time = date('h:i:s a');` and if we use `>= , <=` in if condition then we'll get accurate answer..your answer was very helpful john... – Prabs Mar 14 '15 at 11:03
  • 1
    For situations where the time you're checking ends up on the next day (for example "if 10pm is greater than 8pm and less than 2am" as @oucil mentions) – there's a helpful function that handles all cases [here](http://stackoverflow.com/a/27134087/756641). – Sean Oct 16 '15 at 18:10
  • Dead Link: [See it in action](http://codepad.viper-7.com/vT3NbQ) – Jeff Puckett Jun 16 '16 at 23:21
  • Example code has missleading time format - altough it is AM/PM, it is telling createFromFormat `H`, it should be telling it `h` , see official documentation for date for format description - http://php.net/manual/en/function.date.php – jave.web Jul 05 '18 at 09:35
2

i would recoment you to first "convert" the strings to integers using substr function and intval.

avter this step you should be able to simply compare the times.

( note that you should use 18:00 instead of 6:00 PM, this would be easyer to handl, therefor you could "detect" with substr if the time is AM or PM, and just add "12" to the number )

i'm pretty shure there are more elegant ways of doing this, but this should just give you an BASIC idea for a SIMPLE way for not advanced php developers.

hope it helps

Ace
  • 1,437
  • 6
  • 28
  • 46