I am trying to write a script that would collect current time and check if the time falls with a particular time range.

Let say the current time is 12:00am and I want to check if the current time falls within the range of 1 pm to 5 pm. I kind of confuse on how to implement this with php.

Recommended Answers

All 6 Replies

Member Avatar

Well, I'm assuming that this is date independent.

$now = date("His");//or date("H:i:s")
$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'

if($now >= $start && $now <= $end){
    echo "Time in between";
    echo "Time outside constraints";

tans diafol I would try this now.

I tried it. But this script does not work for me diafol.

Member Avatar

It works for me, but you have to check the system time vs. your time. This is sometimes messed up by daylight saving time. You can set your timezone in php.

So just echo $now to see if it matches your time.


Time is a relative thing... consider using GMT time.
Also keep in mind that any date, time, datetime can be converted to an integer.
And comparing integers is easy :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.