how to strtotime until another strtotime?
I am still learning php language right now, what I want to do is in if..else statement, but in if statement I need to check the thistime until untilthis time only available else error,can I know how to let this code done开发者_运维技巧?
my code:
$thistime=date("h:i:s",strtotime("10:00 am"));
$untiltime=date("h:i:s",strtotime("11:00 am"));
if(){ echo blablabla; } else{echo error;};
thank you and hope you guy reply me soonest
I assume you're trying to only allow access to certain content for a certain amount of time each day, specifically between the hours of 10am and 11am. If that's the case, then you're on the right track, but I'll try to clarify a few things for you.
The strtotime()
function interprets a string to be a given second in time, and returns a Unix timestamp (the number of seconds since midnight on Jan. 1, 1970 in the UTC timezone). You can use these timestamps to set the start and end markers for your time range.
The date()
function acts to format a timestamp into a particular format. The h:i:s
format in your code refers to the current hour, minute, and second of the day, with colon separators between each value. While this function is useful, it serves no purpose in what you're attempting to do, since the timestamps are better options for comparisons.
The last function you need to understand is time()
. This function returns a Unix timestamp for the moment the code runs. You will compare this value to the start and end points defined with strtotime()
to determine whether or not to display your page.
Putting all of that together, we get the following code:
// define the range in which the page is allowed to display
$rangeStart = strtotime("10:00 AM");
$rangeEnd = strtotime("11:00 AM");
// define a variable with the current time
$now = time();
// compare the current time to the two range points
// to determine if the current time is within the display range
if ($now >= $rangeStart && $now <= $rangeEnd) {
// display page
} else {
// display error message
}
Note that in the above code strtotime()
, because no date information is given, is acting under the assumption that 10:00 AM
and 11:00 AM
are times on the current day. This means that that code will be accessible between 10 and 11 AM everyday. If this isn't what you want, then you'll have to be a little more specific on your end points. Also note that strtotime()
acts against whatever timezone your server is set to. This means that if your server is set to be on Pacific time, and you're in New York, then from your perspective you'll only be allowed to access the site between 1 PM and 2 PM local time.
Very simple,
$thistime = date("h:i:s", strtotime("10:00 am"));
$untiltime=date("h:i:s", strtotime("11:00 am"));
if(strtotime("10:00 am") < strtotime("11:00 am"))
{
echo "blablabla";
} else {
echo "error";
};
精彩评论