• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP + Time [SOLVED -- Thanks Dog Cow!]

#1
[eluser]codelearn[/eluser]
Quick question for the gurus out there:

Is there a way to build an if / else script to do one thing between 11am every tuesday to 3am every wednesday?

So between:

Tuesday - 11am
Wednesday - 3am

Run the if, and any other time run the else?

Any help would be SO much appreciated.

#2
[eluser]Dog Cow[/eluser]
Yes, this is possible.

You would need to take the current time() format it to get the day, then hour. First check day, then the hour.

Code:
// using request time here to save a syscall
// this only works on PHP 5.1 or greater, so use time()
// in all other cases.
$time = $_SERVER['REQUEST_TIME'];

$today = date('N', $time); // This gives 1 = Mon, 2= Tues, etc..

$hour = date('G', $time); // this will give 24-hour Military time (0-23)

if ( ( $hour >= 11 && $today == 2) || ( $hour <= 3 && $today == 3) )
{
   do_stuff(); // stuff done between 11 AM Tuesday through to 3 AM Wednesday
}
else
{
   do_something_else(); // stuff done all other times
}

#3
[eluser]codelearn[/eluser]
Have you seen any examples of this done? I'm not really sure how to check a timestamp. Is there any doc on this?

Thanks

#4
[eluser]Dog Cow[/eluser]
I was editing my post. It should be ready now.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.