skip to Main Content

As per this question I have found some code that will display all the Sundays in a given month.

<?php
function getSundays($y, $m)
{
    return new DatePeriod(
        new DateTime("first sunday of $y-$m"),
        DateInterval::createFromDateString('next sunday'),
        new DateTime("last day of $y-$m")
    );
}


foreach (getSundays(date('Y'), date('m')) as $sunday) {
    echo $sunday->format("l, Y-m-dn");
}
?>

What I would like to get is the last four Sundays up until todays date.

I’ve used Moment.js in the past as part of another jQuery & Twitter Bootstrap plugin and think that on the front end at least this is what could be used.

Let’s say for example that today is Wednesday 13 May 2015. The last four Sundays will be

  • 10 May 2015
  • 3 May 2015
  • 26 April 2015
  • 19 April 2015

Let’s say today is Sunday 19 May, then the last four will be

  • 17 May 2015
  • 10 May 2015
  • 3 May 2015
  • 26 April 2015

This is for a simple timesheet application as part of a form. The backend is Laravel 5 (PHP) and the front end so far is simple vanilla JavaScript with some jQuery where necessary. I don’t mind how it is achieved as once I have the dates I can manipulate as necessary.

4

Answers


  1. Chosen as BEST ANSWER

    As I am using Laravel I thought I should be able to use Carbon (I was right). I have posted this answer for myself for future reference and for others.

    @for ($i = 1; $i < 5; $i++)
        {!! CarbonCarbon::now()->endOfWeek()->subWeeks($i) !!}
    @endfor
    

    And with the nice format

    @for ($i = 1; $i < 5; $i++)
        {!! CarbonCarbon::now()->endOfWeek()->subWeeks($i)->format('D j M') !!}
    @endfor
    

  2. You can get back from the previous week then from there, walk with that period you already have then interval next sunday:

    function getLastSundays()
    {
        $from = new DateTime();
        $from->modify('-4 weeks');
        return new DatePeriod(
            $from,
            DateInterval::createFromDateString('next sunday'),
            new DateTime()
        ,DatePeriod::EXCLUDE_START_DATE);
    }
    
    $p = getLastSundays();
    foreach (array_reverse(iterator_to_array($p)) as $sunday) {
        echo $sunday->format("l, d F Y"), '<br/>';
    }
    

    Sample Output

    Login or Signup to reply.
  3. A littel shorter solution:

    $dt = new DateTime("next sunday");
    for ($index = 0; $index < 4; $index++) {
        var_dump($dt->sub(new DateInterval('P7D')));
    }
    
    Login or Signup to reply.
  4. Here is a javascript solution.
    I put some extra code in to find the current Year’s end of year date.
    can leave that out and input the date directly.

    function displayLastFourSundays()
    {
        var endOfCurrentYear = new Date();
        endOfCurrentYear.setFullYear((new Date().getFullYear()));
        endOfCurrentYear.setMonth(11);//months from 0 - 11  (0.o)
        endOfCurrentYear.setDate(31);
        //got end of year this way^
        
        var dateArr = new Array();
        var startDate = new Date(endOfCurrentYear.setDate(-5));//sets to 5th last day of previous month ie. Nov 25th to start at 5 weeks before the end of the year
        //create a new instance of date with end of year
        var tempDate = new Date(startDate.toString());
        for (var i = 0; i <= totalDays; i++)
        {
            tempDate.setDate(tempDate.getDate()+1);
            if (tempDate.getDay() == 0) // if sunday
                dateArr.push(new Date(tempDate));
        }
        var lastFourSundays = new Array();
        var numOfSun = dateArr.length, count = 0;
        for(var i = numOfSun - 4 , l = numOfSun; i < l; i++)
        {
            lastFourSundays.push(dateArr[i]);
                console.log("vvvvvvvvvvvvvvvvvvvvnfound Sunday " + dateArr[i] + "n^^^^^^^^^^^^^^^^^^^^");//do something with sunday
            if(count++ > 10)
                break;
        }
    }
    
    displayLastFourSundays();//execute our function
    

    gets you an array with sundays and outputs:

    vvvvvvvvvvvvvvvvvvvv

    found Sunday Sun Dec 06 2015 13:13:33 GMT+0200 (South Africa Standard Time)

    ^^^^^^^^^^^^^^^^^^^^

    vvvvvvvvvvvvvvvvvvvv

    found Sunday Sun Dec 13 2015 13:13:33 GMT+0200 (South Africa Standard Time)

    ^^^^^^^^^^^^^^^^^^^^

    vvvvvvvvvvvvvvvvvvvv

    found Sunday Sun Dec 20 2015 13:13:33 GMT+0200 (South Africa Standard Time)

    ^^^^^^^^^^^^^^^^^^^^

    vvvvvvvvvvvvvvvvvvvv

    found Sunday Sun Dec 27 2015 13:13:33 GMT+0200 (South Africa Standard Time)

    ^^^^^^^^^^^^^^^^^^^^

    to the console

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search