Forum Moderators: coopster

Message Too Old, No Replies

split a date range into individual weeks

         

bilenkyj

9:12 am on Aug 27, 2008 (gmt 0)

10+ Year Member



Hi Guys, long time no speak.

im looking to create a kinda gantt chart effect on screen by taking a date range and splitting it into its individual weeks

04/01/2008 - 30/01/2008
so this would be weeks 1 - 4 (im looking for yyyy/ww format)
2008/01
2008/02
2008/03
2008/04

I would then have a table with columns for 52 weeks of the year, i can then colour in the date range on the table - colour weeks 1 - 4

could anyone set me off in the right direction with doing the split correctly?

cameraman

9:22 am on Aug 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yikes I'm from the US. I stared at that for quite a while wondering how in the heck April is week 1.
If you swap the format around,
$target = strtotime('yyyymmdd');
will give you the timestamp for the date, then
date('W',$target)
will give you the week number of the year.

bilenkyj

9:41 am on Aug 27, 2008 (gmt 0)

10+ Year Member



nice, thats the first thing, yeah im from the UK, i should have put it in mysql date format, sorry, thank you anyways

g1smd

9:54 am on Aug 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RFC 3339 suggests that using YYYY-MM-DD date format everywhere on the planet removes the ambiguity between the US-centric mm/dd/yyyy and the dd/mm/yyyy formats used elsewhere. That suggestion is derived from ISO 8601 which 99% of the planet signed up to several decades ago.

In the US also see ANSI X3.30 and NIST FIPS 4-1. In the UK also see BS EN 28601, and EN 28601 throughout Europe.

bilenkyj

10:00 am on Sep 3, 2008 (gmt 0)

10+ Year Member



im looking for something additional,
i need a row of 52 weeks - each with the monday date of the week and the week number next to it - i can get a week number with the date("W") function but i need the week monday date - any ideas?

is there something i can do with
strtotime

monday of week 23 kind of thing?

mrscruff

10:43 am on Sep 3, 2008 (gmt 0)

10+ Year Member



Hello, just a quick idea.

using date('N'), you can also get the day of the week (1-Mon through 7-Sun).

Then useing something like '1 - date('N', timestamp)', 0 meaning the day is monday else the difference.

then with your date use strtotime("date -{difference} days")

or 'if (difference > 0) strtotime("date last Monday")'

Hope this helps.

bilenkyj

11:00 am on Sep 3, 2008 (gmt 0)

10+ Year Member



managed to stick something together in the end -

<?php

$target = strtotime("2008-01-01");
echo"<br>".$target;
$thedate = date('W',"$target");
echo"<br>".$thedate;
echo"<Br>".date('W');

function week($year, $week)
{
$from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7")); //Returns the date of sunday in week
return "Week {$week} in {$year} is from {$from} to {$to}.";
}

$counter=1;

while($counter!==53){
echo"<br>".$counter;

if(strlen($counter)==1){
$string1=0;
$correntcounter=$string1.$counter;
}

if(strlen($counter)==2){

$correntcounter=$counter;
}

echo"<br>".week(2008,$correntcounter);
//Returns: Week 27 in 2008 is from 2008-06-30 to 2008-07-06.
$counter++;
}

?>