1

I've been working at this code off and on for the past few days and can't figure it out.

What I need to do is return from a function either a 0 or 1 depending on if the current time is within the times set by a user. If the time and date is within a 4 value array set by the user, then return 1, if not, return 0. The user can set multiple arrays for multiple periods of times.

I've been trying to work with this code for a while:

functions.php:

function determineWoE($woe) {
  $curDayWeek = date('N');
  $curTime = date('H:i');
  $amountWoE = count($woe['WoEDayTimes']); // Determine how many WoE times we have.
  if ( $amountWoE == 0 ) {
    return 0; // There are no WoE's set! WoE can't be on!
  }
  for ( $i=0; $i < $amountWoE; $i++ ) {
    if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.
      if ( $woe['WoEDayTimes'][$i][1] >= $curTime && $woe['WoEDayTimes'][$i][3] <= $curTime ) { // Check current time of day.
        // WoE is active
        return 1;
      }
      else {
        // WoE is not active
        return 0;
      }
    }
    else {
      // WoE is not active
      return 0;
    }
  }
}

And...where the user sets as many periods of time for this feature that they want:

$woe = array( // Configuration options for WoE and times.
  // -- WoE days and times --
  // First parameter: Starding day 1=Monday / 2=Tuesday / 3=Wednesday / 4=Thursday / 5=Friday / 6=Saturday / 7=Sunday
  // Second parameter: Starting hour in 24-hr format.
  // Third paramter: Ending day (possible value is same or different as starting day).
  // Fourth (final) parameter: Ending hour in 24-hr format.
  'WoEDayTimes'   => array(
    array(6, '18:00', 6, '19:00'), // Example: Starts Saturday 6:00 PM and ends Saturday 7:00 PM
    array(3, '14:00', 3, '15:00')  // Example: Starts Wednesday 2:00 PM and ends Wednesday 3:00 PM
  ),
);

But, no matter what I do...the function determineWoE always returns 0.

Am I needing a foreach in the function instead of a for? How do I get determineWoE to return 1 if the time is within the user settable times?

Tried changing the for to a foreach:

foreach ( $woe['WoEDayTimes'] as $i ) {

And now I get error: Warning: Illegal offset type in /var/www/jemstuff.com/htdocs/ero/functions.php on line 76

...which I have no idea why I would be getting that error. Line 76 is:

if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.

In functions.php

var_dump($woe)

array(2) { ["WhoOnline"]=> string(2) "no" ["WoEDayTimes"]=> array(2) { [0]=> array(4) { [0]=> int(6) [1]=> string(5) "18:00" [2]=> int(6) [3]=> string(5) "19:00" } [1]=> array(4) { [0]=> int(3) [1]=> string(5) "14:00" [2]=> int(3) [3]=> string(5) "15:00" } } }

Thanks for any help you can provide to me. :)

Jguy
  • 583
  • 1
  • 11
  • 33
  • It's close...I can figure out how to get the time range, it's just really the array that I think it giving me the issues. Array's and myself don't get along ;) – Jguy Jun 03 '12 at 00:19
  • What is the output of: `var_dump($woe);`? – PeeHaa Jun 03 '12 at 00:24
  • var_dump($woe) = array(2) { ["WhoOnline"]=> string(2) "no" ["WoEDayTimes"]=> array(2) { [0]=> array(4) { [0]=> int(6) [1]=> string(5) "18:00" [2]=> int(6) [3]=> string(5) "19:00" } [1]=> array(4) { [0]=> int(3) [1]=> string(5) "14:00" [2]=> int(3) [3]=> string(5) "15:00" } } } – Jguy Jun 03 '12 at 00:31
  • Comments are not the place for a var_dump(). If the question needs more clarification, please add this to the question. – ghoti Jun 03 '12 at 01:07
  • Sorry! Edited original question. – Jguy Jun 03 '12 at 01:16
  • Your missing a closing parentheses on your $woe? Copy/Paste error? – Craig Stewart Jun 03 '12 at 01:20
  • After array(3, '14:00', 3, '15:00') – Craig Stewart Jun 03 '12 at 01:21
  • Indeed. Copy/paste error. Fixed. Sorry. – Jguy Jun 03 '12 at 01:29
  • Your conditional statement if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) says both the start and end day must be the same as the current day of the week. But you say they can be different. That statment is only ever true if both start and date are the same – Craig Stewart Jun 03 '12 at 01:34
  • if( $woe['WoEDayTimes'][$i][0] >= $curDayWeek && $woe['WoEDayTimes'][$i][2] <= $curDayWeek ) – Craig Stewart Jun 03 '12 at 01:38
  • More on var_dump(). One of the reasons it shouldn't go in comments is that comments lose formatting. When var_dump() is all on one long line, it's very difficult to read. If you want better answers, make your questions easier to read. – ghoti Jun 03 '12 at 02:23

1 Answers1

2

A couple minor points:

  • A foreach loop and a for loop would both work fine, but you might find the foreach more covenient, since you wouldn't have to count() the days/times to check for.

  • You should return boolean true or false instead of 1 or 0.

I'm not sure why you're getting that error, but the bigger problem I see is how you compare the times. You cast the string times to numeric types, and that won't convert entirely like you think it will. For example...

"14:00" < "14:59"

...Would be false, because it casts both strings to 14. Thus, the first string actually equals the second.

You might be better off converting the strings to Unix Timestamps (which are the seconds since 1/1/1970), and comparing those.

Here's a rough idea of how I would do it:

// Function to help get a timestamp, when only given a day and a time
// $today is the current integer day
// $str should be 'last <day>', 'next <day>', or 'today'
// $time should be a time in the form of hh:mm
function specialStrtotime($today, $day, $time) {

    // An array to turn integer days into textual days
    static $days = array(
        1 => 'Monday',
        2 => 'Tuesday',
        3 => 'Wednesday',
        4 => 'Thursday',
        5 => 'Friday',
        6 => 'Saturday',
        7 => 'Sunday'
    );

    // Determine if the day (this week) is in the past, future, or today
    if ($day < $today) {
        $str = 'last ' . $days[$day];
    } else if ($day > $today) {
        $str = 'next ' . $days[$day];
    } else {
        $str = 'today';
    }

    // Get the day, at 00:00
    $r = strtotime($str);

    // Add the amount of seconds the time represents
    $time = explode(':', $time);
    $r += ($time[0] * 3600) + ($time[1] * 60);

    // Return the timestamp
    return $;
}

// Your function, modified
function determineWoE($timeNow, $woe) {
    $dayNow = (int) date('N', $timeNow);
    foreach ($woe as $a) {
        // Determine current day

        // Determine the first timestamp
        $timeFirst = specialStrtotime($dayNow, $a[0], $a[1]);

        // Determine the second timestamp
        $timeSecond = specialStrtotime($dayNow, $a[2], $a[3]);

        // See if current time is within the two timestamps
        if ($timeNow > $timeFirst && $timeNow < $timeSecond) {
            return true;
        }
    }
    return false;
}

// Example of usage
$timeNow = time();
if (determineWoE($timeNow, $woe['WoEDayTimes'])) {
    echo 'Yes!';
} else {
    echo 'No!';
}

Good luck!

Litty
  • 1,856
  • 1
  • 16
  • 35
  • Hi, thanks! One issue I have found is your specialStrtotime function is calling unreferenced variable $a...I see you've referenced that in the other function...I'm not sure what or how you're doing what you're doing, so I'm not able to fix it. Thanks for an explanation if you can provide one! – Jguy Jun 03 '12 at 02:34
  • 1
    No worries! My mistake, $a[0] is supposed to be $day. I've edited the answer to reflect it. – Litty Jun 03 '12 at 02:42
  • Glad to hear! Good luck with the rest of your project! – Litty Jun 03 '12 at 02:44