Sushant Aryal
Sushant Aryal

Reputation: 3335

How to generate a weekly report?

i was able to generate a weekly report using some date function. The table looks like

-------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday  |Saturday |
     |July 21 |July 22 |July 23 |July 24   |July 25  |July 26 |July 27  |
-------------------------------------------------------------------------

but whenever the current week consists of 2 months, like end of July and start of August. i am unable to find the current week. The table looks like

-------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday  |Saturday |
     |July 28 |July 29 |July 30 |July 31   |July 32  |July 33 |July 34  |
-------------------------------------------------------------------------

please help. The result should look like

--------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday   |Saturday |
     |July 28 |July 29 |July 30 |July 31   |August 1 |August 2 |August 3 |
--------------------------------------------------------------------------

Upvotes: 3

Views: 4854

Answers (4)

giorgio
giorgio

Reputation: 10212

First grab the monday:

$monday = new DateTime('monday this week');
// OR
$monday = new DateTime('may 28th 1983'); // no that's NOT my birthday ;)
$monday->modify('monday this week');

Then just loop 7 times

for($i=0; $i<7; $i++) {
    echo $monday->format('Y-m-d') .'<br>';
    $monday->modify('+1 day');
}

But: please note that weeks start on sunday as far as php is concerned. This should be taken into account if you want the first day of the week starting on monday.

Example given: using new DateTime('july 21th 2013') and modify it to monday this week will result in july the 22th, while you'd expect it to be the 15th. So an extra check is needed in this case:

if($monday->format('l') == 'Monday') {
    // don't modify the date
}

Upvotes: 0

dognose
dognose

Reputation: 20909

I assume you have the first day of the week as a starting point?

Then use phps stringtotime to easily progress in days:

<?php
   $startOfWeek = date("Y-m-d", strtotime("Monday this week"));

   for ($i=0; $i<7;$i++){
       echo date("l, d M", strtotime($startOfWeek . " + $i day"))."<br />";
   }
?>

Output:

Monday, 29 Jul
Tuesday, 30 Jul
Wednesday, 31 Jul
Thursday, 01 Aug
Friday, 02 Aug
Saturday, 03 Aug
Sunday, 04 Aug

format as required.

Update to your Question:

//find week start
$weekstart = date("Y-m-d", strtotime("Monday this week"))   ;
echo $weekstart; 

strtotime is pretty flexible .

Upvotes: 4

Sherin Jose
Sherin Jose

Reputation: 2516

You can use the following code to get the week dates for current date.....

<?php
//current time stamp
$ts = time();
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
    echo date("m/d/Y l", $ts) . "<br/>";
}
?>

Refer the link

Upvotes: 0

Jakub Matczak
Jakub Matczak

Reputation: 15676

Example from php.net:

<?php
$currentdate  = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
    echo $day_eg1 = date ('N',$currentdate);
    echo $day_eg2 = date("N", $today+1 * 24 * 3600);
    echo $day_eg3= date("N", $today+2 * 24 * 3600);
    echo $day_eg4 = date("N", $today+3 * 24 * 3600);
    echo $day_eg5 = date("N", $today+4 * 24 * 3600);
    echo $day_eg6 = date("N", $today+5 * 24 * 3600);
    echo $day_eg7 = date("N", $today+6 * 24 * 3600);
?>

Source: http://php.net/manual/pl/function.date.php

It counts 7 days from today, do all you have to do is count Monday time if you want to create this report for example for last week.

Upvotes: 0

Related Questions