Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Code to add a class when a field is empty or between certain times. As it spans over days it's difficult to work out

$
0
0

I'm trying to figure out a way to add classes, when a field is empty, when a time range is open, when a time range is within 2 hours of closing time, when a time range is within 1 hour of closing time, and when a time range shows when something is closed.

I know what I want to do but can't figure out the logic as sometimes the time range spans across days. Any help would be greatly appreciated.

I just want to type in when a place is closed and have the code add one of 4 classes.gray: an empty fieldmaybe-open: not withing the range submittedclosed: withing the range submittedyellow: 2 hours before the rangeorange: 1 hour before the range

Here is my code so far.

The range will look like this: 9pm - 10:30am

// Calculate opening status and add custom classes based on time range and current timefunction custom_post_class($classes, $class, $post_id) {    // Retrieve closing hours from field    $closing_hours = get_field('field_65dfbc30e1727', $post_id); // Adjust field key as per your setup    // If closing hours are empty, add 'gray' class to all items and return    if (empty($closing_hours)) {        $classes[] = 'gray';        return $classes;    }    // Get current time in desired format    date_default_timezone_set('Asia/Tokyo');    $current_time = strtotime(date('H:i'));    // Parse closing hours into start and end times    $times = explode(' - ', $closing_hours);    $closing_start_time = strtotime($times[0]);    $closing_end_time = strtotime($times[1]);    // Check if the closing time is on the next day    if ($closing_start_time > $closing_end_time) {        // Adjust closing end time to be on the next day        $closing_end_time = strtotime('+1 day', $closing_end_time);    }    // Check if the current time is within open hours    if ($current_time < $closing_start_time || $current_time > $closing_end_time) {         // Add 'closed' class if the current time is outside of the closing hours range        $classes[] = 'maybe-open';    } elseif (($current_time >= $closing_start_time && $current_time <= strtotime('23:59', $closing_end_time)) ||               ($current_time >= strtotime('00:00', $closing_start_time) && $current_time <= $closing_end_time)) {         // Add 'maybe-open' class if the shop might be open        $classes[] = 'closed';    }    // Calculate time differences    $time_diff_current = $closing_end_time - $current_time; // Difference between closing time and current time    // Add 'yellow' class if the current time is within 2 hours of the range    if ($time_diff_current <= 7200 && $time_diff_current > 3600) {       $classes[] = 'yellow';    }    // Add 'orange' class if the current time is within an hour of the range    if ($time_diff_current <= 3600 && $time_diff_current > 0) {        $classes[] = 'orange';    }    // Add 'closed' class if the current time is exactly at the closing time    if ($time_diff_current == 0) {        $classes[] = 'closed';    }    return $classes;}

As it's a bit complicated I'm using the questions to go through the logic.

  1. If the current time is 10:01pm and the closing time is 9pm is the class "closed"?
  2. If the current time is 10:01pm and the closing time is 11pm is the class "yellow"?
  3. If the current time is 10:01pm and the closing time is 12am is the class "orange"?
  4. If the current time is 10:01pm and the closing time is 1am is the class "maybe-open"?
  5. If the current time is 10:01pm and the closing time is 1pm to 2pm is the class "maybe-open"?
  6. If the field is empty is the class "gray"?
  7. If the current time is 10:01pm and the closing time is 10am to 3pm, the same as question 5, the class must be "maybe-open", right?

I tried using ChatGTP as well but the code seems to be too difficult for it. And it keeps showing me broken code or repeating to show me code that works in the morning but not at night for some reason.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>