.mktime and .strftime help returning wrong day of

2019-09-02 03:27发布

问题:

Before I get yelled at I am not a php coder and I have searched vary heavily to try and figure this out without luck here and on the net. I have seen some things, but just don't have enough understanding to apply with my limited understanding. The php code was developed by someone who has vanished, so not supporting and I have one little bug I need to try and clean up and why a html/css guy is even trying to figure this out without much luck. So appreciate groups help and patience as well as specificity if possible.

Problem: the php code is returning the wrong day of the week on a statistics report the software generates. So when you go run the statistics page and it queries the DB it returns a value but the wrong day label shows up.

In my test case I have data only on Thursday of the month and when I run the report it shows up as data for Sunday.

Here are what I assume are the two section of code from my research.. rather than posting the entire php file. I guess at least I think I narrowed down the code section...small victories.

The code used to get the first day of the month is (which i have seen out there and compared and looks like it is correct):

    function get_first_day($day_number=1, $month=false, $year=false)
{
  $month  = ($month === false) ? strftime("%m"): $month;
  $year   = ($year === false) ? strftime("%Y"): $year;

  $first_day = 1 + ((7+$day_number - strftime("%w", mktime(0,0,0,$month, 1, $year)))%7);

  return mktime(0,0,0,$month, $first_day, $year);
}

Before I go on I have also tried to set $day_number=1 to various values 0-6, but seems to have no effect. I read several post that setting that value was the problem, but made no difference for me. I thought maybe the "session" was causing the issue after trying to learn about them, so I tried to log out and log back in clear my cache and also change browsers after changing the value but that got me nowhere.

The code I guess you would say that calls this??? to generate the output report that is not correct is:

<table id="graph_weekday" class="data" style="display:none" cellpadding="0" cellspacing="0" width="100%">
        <caption><?php echo _occupancy_per_week." / "._days;?></caption>
        <thead>
            <tr>
                <td class="no_input">&nbsp;</td>
                <?php
                foreach ($label_wk as $value) {
                    echo "<th>".strftime("%A", get_first_day($value, $_SESSION['statistic_month'], $_SESSION['selectedDate_year']))."</th>";
                }
                ?>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th><?php echo _days;?></th>
                <?php
                foreach ($data_wk as $value) {
                    echo "<td>".$value."</td>";
                }
                ?>
            </tr>
        </tbody>
    </table>
                    }
                    ?>

So things seem to be working to pull the data, but the label is wrong. Though setting the value for $day_number= to something else doesn't seem to work.

Is there something wrong with the.$value. which is being echoed and I need to apply the get_first_day to the value?

update: Trying to dig into this, but still need help I found this within the code that I think is also relivent (maybe)

// Bar Plot Guest by weekday/month
$data_wk = array();
$label_wk = array();

    $statistic = querySQL('statistic_weekday');

    foreach ($statistic as $key => $value) {
        foreach($value as $paxsum){
            $label_wk[] = $key;
            $data_wk[] = $paxsum;
        }
    }

Thanks for any help.

回答1:

Ok I think I have it. I was changing the value $day_number=1 to a different number. I wasn't changing the $first_day = 1. When I changed $first_day = 5 it seems to work. Does this seem like it is correct or going to cause me other issues?

  function get_first_day($day_number=1, $month=false, $year=false)
  {
    $month  = ($month === false) ? strftime("%m"): $month;
    $year   = ($year === false) ? strftime("%Y"): $year;

    $first_day = 5 + ((7+$day_number - strftime("%w", mktime(0,0,0,$month, 1, $year)))%7);

    return mktime(0,0,0,$month, $first_day, $year);
  }