diff --git a/habit.php b/habit.php index 7737e58..2be55ba 100644 --- a/habit.php +++ b/habit.php @@ -229,7 +229,7 @@ $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60)); // Check if the week is more than 24 weeks ago - if ($dayDifference > 10) { + if ($dayDifference > 9) { break; } @@ -495,67 +495,61 @@ $first = ""; foreach ($lines as $line) { - // Extract the date from the line - preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); - $date = isset($matches[0]) ? $matches[0] : null; + // Extract the date from the line + preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; - // Explode the line into words - $words = explode(' ', $line); + // Explode the line into words + $words = explode(' ', $line); - // Check if the line contains the specified word as a hashtag - $wordFound = false; - foreach ($words as $wordInLine) { - // Check if the word contains "#takeout" (case-insensitive) - if (stripos($wordInLine, "#$word") !== false) { - $wordFound = true; - $first = $date; // $first will end up containging first instance of word - break; - } - } + // Check if the line contains the specified word as a hashtag + $wordFound = false; + foreach ($words as $wordInLine) { + // Check if the word contains "#takeout" (case-insensitive) + if (stripos($wordInLine, "#$word") !== false) { + $wordFound = true; + $first = $date; // $first will end up containing the first instance of the word + break; + } + } - // Populate the dateOccurrences array - if ($date !== null && $wordFound) { - if (!isset($dateOccurrences[$date])) { - $dateOccurrences[$date] = 0; - } - $dateOccurrences[$date]++; - } - - // Output the line whether the word is found or not - //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; - } - - // Output the occurrences - //print_r($dateOccurrences); + // Populate the dateOccurrences array + if ($date !== null && $wordFound) { + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + $dateOccurrences[$date]++; + } + } // Create weekly occurrences array - foreach ($dateOccurrences as $date => $count) { - // Convert each date to the format $year-$weekNo - $dateTime = new DateTime($date); - $dateTime->modify('this week sunday'); // Set to Sunday of the current week - $yearWeek = $dateTime->format('Y-m-W'); - - // Add +1 for each date converted into $year-$weekNo as a key - if (isset($weeklyOccurrences[$yearWeek])) { - $weeklyOccurrences[$yearWeek]++; - } else { - $weeklyOccurrences[$yearWeek] = 1; - } - } - - $today = new DateTime(); - $currentDate = date('Y-m-d'); - - while (strtotime($first) <= strtotime($currentDate)) { + // Convert each date to the format $year-$weekNo $dateTime = new DateTime($date); $dateTime->modify('this week sunday'); // Set to Sunday of the current week $yearWeek = $dateTime->format('Y-m-W'); - $weeklyOccurrences[$yearWeek] = 0; - //echo $yearWeekKey; - $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day')); + + // Add +1 for each date converted into $year-$weekNo as a key + if (isset($weeklyOccurrences[$yearWeek])) { + $weeklyOccurrences[$yearWeek]++; + } else { + $weeklyOccurrences[$yearWeek] = 1; + } } + // Fill in missing weeks up to the current week + $today = new DateTime(); + $currentDate = date('Y-m-d'); + + while (strtotime($currentDate) >= strtotime($first)) { + $dateTime = new DateTime($currentDate); + $dateTime->modify('this week sunday'); // Set to Sunday of the current week + $yearWeek = $dateTime->format('Y-m-W'); + if (!isset($weeklyOccurrences[$yearWeek])) { + $weeklyOccurrences[$yearWeek] = 0; + } + $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day')); + } $consecutiveCount = 0; $maxConsecutiveCount = 0; @@ -567,14 +561,16 @@ $consecutiveCount++; $maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount); } else { - $consecutiveCount = 0; // Reset count if the value is less than 2 + $consecutiveCount = 0; // Reset count if the value is less than the multiplier + } + + // If the current week's count is less than the multiplier and it's the current week, reset the streak + if ($value < $multiplier && $week === date('Y-m-W')) { + $maxConsecutiveCount = 0; } } - //print_r($dateOccurrences); - //print_r($weeklyOccurrences); - - return $consecutiveCount; + return ($maxConsecutiveCount >= $multiplier) ? $maxConsecutiveCount : 0; } function habit_get_stat_missed($word, $filename) {