Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:39:39+00:00 2026-06-01T17:39:39+00:00

I’m currently struggling with what should be a basic task. I have a start

  • 0

I’m currently struggling with what should be a basic task. I have a start date, an end date and a count. I need to calculate how many times an hour the third parameter (the count) should decrease between the two dates: like so, countOffers("2012-03-27 11:00:00", "2012-04-08 19:00:00", 200) every hour.

That bit, I think we have covered OK. The problem comes now.

We only want the counting to occur when our website is open. These times are stored in an array, the 0 index is open and 1 is close. Also, the date is dynamically updated to now.

Array
(
    [mon] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 21:30
        )

    [tue] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 21:30
        )

    [wed] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 21:30
        )

    [thu] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 21.30
        )

    [fri] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 19:00
        )

    [sat] => Array
        (
            [0] => 2012-04-03 9:00
            [1] => 2012-04-03 18:00
        )

    [sun] => Array
        (
            [0] => 2012-04-03 10:30
            [1] => 2012-04-03 19:00
        )

)

So every hour the counter will decrease, whilst we’re between the opening times. When we’re closed however, we need to calculate where the counter would be up to the closing time today.

openTimes has a variable called areWeOpen which we can use to check if we’re currently open or closed. We have some code however it doesn’t always seem to work:

function countOffers($start, $end, $deals) {
    global $openTimes;

    if(strtotime($end) < time()) return 1;

    define('ONEHOUR', 1);

    $totalDays   = unixtojd(strtotime($end)) - unixtojd(strtotime($start));
    $daysBefore  = unixtojd(time()) - unixtojd(strtotime($start));
    $daysAfter   = unixtojd(strtotime($end)) - unixtojd(time());
    $startDay    = strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)))));
    $totalHours  = 0;
    $hoursBefore = 0;

    /* TOTAL HOURS */
    for($i = 0; $i <= $totalDays; $i++) {
        $dayName = strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days")));
        $day = $openTimes->openDays[$dayName];
        if($i === 0) {
            $startHour = explode(" ", $start);
            $startHour = str_replace(array(":","3"), array(".","5"), $startHour[1]);
            $endHour = explode(" ", $day[1]);
            $endHour = str_replace(array(":","3"), array(".","5"), $endHour[1]);
            $totalHours += $endHour - $startHour;
        } else {
            $tempHour = (strtotime($day[1]) - strtotime($day[0])) / 3600;
            $totalHours += (strtotime($day[1]) - strtotime($day[0])) / 3600;
        }
    }
    $perHour = round($deals / $totalHours, 1);

    $today = 0;
    if($openTimes->areWeOpen === FALSE && $openTimes->morning === FALSE) {
        /* HOURS UP TO TODAY */
        for($i = 0; $i < $daysBefore; $i++) {
            $day = $openTimes->openDays[strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days")))];
            $hoursBefore += (strtotime($day[1]) - strtotime($day[0])) / 3600; 
        }
    } elseif (strtotime($start) <= time()) {
        /* HOURS UP TO YESTERDAY */
        for($i = 0; $i < ($daysBefore-1); $i++) {
            $day = $openTimes->openDays[strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days")))];
            $hoursBefore += (strtotime($day[1]) - strtotime($day[0])) / 3600; 
        }

        if(strstr($start, date("Y-m-d", time()))) {
            $today = ceil((time() - strtotime($start)) / 3600) - ONEHOUR;
        } else {
            $today = ceil((time() - strtotime($openTimes->openDays[strtolower(date("D", time()))][0])) / 3600) - ONEHOUR;
        }
    }
    $alreadyGone = $hoursBefore*$perHour;

    $dealsLeft = $deals - (($hoursBefore*$perHour) + ($today*$perHour));
    if($dealsLeft < 0.5) $dealsLeft = 1;

    return round($dealsLeft);
}

It’ll count properly, however it seems to struggle when we’re closed, it’ll continue to decrease. I know there must be a better way to do this, I just can’t figure it out. I’m over complicating the problem in my head too much I think.

*Edit: * Okay, here is a break down of what I’m trying to achieve:

During opening times (provided in an array) I need to decrease a value x amount of times during the day. If we’re closed, then we only want to decrease that value up until the last closing time.

We are given a start date, end date and the start number of the counter. Between say 9AM and 9PM each day, the value can decrease. If it’s past that time, or we’re currently closed, we only want to decrease till the last closing time (potentially yesterday).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:39:41+00:00Added an answer on June 1, 2026 at 5:39 pm

    This is not the prettiest or most efficient code, but I think it does what you are asking for.

    <?php
    /* test data
    $openTimes->openDays = array(
    'mon' => array('2012-03-27 09:00', '2012-03-27 21:30'),
    'tue' => array('2012-03-27 09:00', '2012-03-27 21:30'),
    'wed' => array('2012-03-27 09:00', '2012-03-27 21:30'),
    'thu' => array('2012-03-27 09:00', '2012-03-27 21:30'),
    'fri' => array('2012-03-27 09:00', '2012-03-27 19:00'),
    'sat' => array('2012-03-27 09:00', '2012-03-27 18:00'),
    'sun' => array('2012-03-27 10:30', '2012-03-27 19:00'),
    );
    */
    
    function countOffers($start, $end, $deals, $now='') {
        $start = new DateTime($start);
        $end = new DateTime($end);
        $now = new DateTime($now);
        if ($now <= $start) {
            return $deals;
        }
        if ($now >= $end) {
            return 0;
        }
        $totalHours = openTimeBetween($start, $end) / 60 / 60;
        $hoursRemaining = openTimeBetween($now, $end) / 60 / 60;
        $perHour = $deals / $totalHours;
        return (int)round($hoursRemaining * $perHour);
    }
    
    function openTimeBetween($start, $end) {
        $totalTime = 0;
        $today = new DateTime($start->format('Y-m-d H:i:s'));
        while ($today <= $end) {
            $totalTime += openTimeRemaining($today, $start, $end);
            // set time to midnight the next day
            $today->setTime(0, 0, 0);
            $today->modify('+1 day');
        }
        return $totalTime;
    }
    
    function openTimeRemaining($current, $minTime, $maxTime) {
        global $openTimes;
        // get the open/close times
        $day = strtolower($current->format('D'));
        list($open, $close) = $openTimes->openDays[$day];
        $open = new DateTime($open);
        $close = new DateTime($close);
        // set the date to be the same as $current
        $open->setDate($current->format('Y'), $current->format('m'), $current->format('d'));
        $close->setDate($current->format('Y'), $current->format('m'), $current->format('d'));
    
        // if it's past closing time or past the maximum time
        if ($current > $close || $current > $maxTime) {
            return 0;
        }
        // if it's the first day, count from $minTime or $current, whichever is later
        else if ($current->format('Y-m-d') === $minTime->format('Y-m-d')) {
            $diff = max($minTime, $current, $open)->diff($close);
        }
        // if it's the last day, count to $maxTime or $close, whichever is earlier
        else if ($current->format('Y-m-d') === $maxTime->format('Y-m-d')) {
            $diff = max($current, $open)->diff(min($maxTime, $close));
        }
        // otherwise count the total open time
        else {
            $diff = $open->diff($close);
        }
        return $diff->h * 60 * 60 + $diff->i * 60 + $diff->s;
    }
    

    To test, you can call countOffers() with a fourth parameter string to use as the current time.

    $start = '2012-03-27 11:00:00';
    $end = '2012-04-08 19:00:00';
    $offers = 200;
    $now = '2012-04-04 17:00:00';
    countOffers($start, $end, $offers, $now);
    

    Leave off the extra parameter to default to the current time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.