PHP minus one week Sunday
<p>I'm a newbie in both php and codeigniter.</p>

<p>I have a function which is used to label the x-axis of a chart of weekly sums of kilometers rode on a bicycle. It works (or is supposed to work) by counting back from today's date either zero Sundays or 1, 2, 3, etc as passed by an array.</p>

<p>I am using PHP Version 5.6.19, and this is my code:</p>

<p>In the Model:</p>

<pre><code>    public function  week_start($date, $counter){
        $tstring = strtotime($date);
        $wknumber = ($counter + 1);
        $start =  strtotime("-$wknumber weeks sunday", $tstring);
    return array(date('M d', $start));}
</code></pre>

<p>In the Controller:</p>

<pre><code>$datenumbers = array(0, 1, 2, 3, 4, 5, 6, 7);
$dateResult = $this-&gt;bike_model-&gt;week_start(date("Y/m/d"), $datenumber);
</code></pre>

<p>In the view: </p>

<pre><code>            for ($i = 0; $i &lt; 8; $i++){
            echo "&lt;text class = \"axis\" y = \"357\" x = \"";
            echo 26 + ($i*70);
            echo "\" dy = \".35em\"&gt;";
            echo $dates_for_table[$i];}
</code></pre>

<p>I have had it up for few days and it has been showing Sunday July 3 as the first number on on my <a href="http://i.imgur.com/tUyAzKz.png" rel="nofollow">x-axis</a>, as it should have. But today, it is Sunday and I expect it to now say Sunday July 10 as my most recent week. However, it is still showing last Sunday!</p>

<p>So my question is, is there a bug here or am I misunderstanding how '- one week Sunday' should work? </p>

<p>My goal is to have my function with zero as the argument return the most recent week start--either the current Sunday if it is Sunday or the past Sunday. I have found a workaround by adding an if else to the function checking if it is Sunday.</p>

<pre><code>if ($tstring == strtotime('this Sunday')){
    $wknumber = $counter;}

else{
$wknumber = ($counter + 1);}
</code></pre>

<p>However, this feels clunky and I'm looking for a more elegant way. I also want to understand what went wrong with "- one week Sunday" since I thought I had understood how it was working.</p>

<p>Thank you!</p>