hopefully sorted?!
1 parent 46ec49a commit 29be2a5d9beb3d3279a02e992afd001602926756
0xRoM authored on 2 Apr
Showing 1 changed file
View
162
habit.php
// Calculate the difference in weeks
$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;
}
 
if (!isset($dateOccurrences[$date])) {
$weeklyOccurrences = [];
$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;
 
// 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;
}
}
 
// 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);
// 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);
 
// 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]++;
}
}
 
// 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;
 
// 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;
 
if ($value >= $multiplier) {
$consecutiveCount++;
$maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount);
} else {
$consecutiveCount = 0; // Reset count if the value is less than 2
}
}
 
//print_r($dateOccurrences);
//print_r($weeklyOccurrences);
 
return $consecutiveCount;
$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;
}
}
 
return ($maxConsecutiveCount >= $multiplier) ? $maxConsecutiveCount : 0;
}
 
function habit_get_stat_missed($word, $filename) {
// Split the file content into an array of lines
Buy Me A Coffee