Jump to content

[SOLVED] start date of a week


abdfahim

Recommended Posts

this is the long way around but i tried.....

 

<?php

$day_now=date("D");

if($day_now=="Mon"){
$x=0;
}

if($day_now=="Tue"){
$x=1;
}

if($day_now=="Wed"){
$x=2;
}

if($day_now=="Thi"){
$x=3;
}

if($day_now=="Fri"){
$x=4;
}
if($day_now=="Sat"){
$x=5;
}

if($day_now=="Sun"){
$x=6;
}

$r=date("-m-y");

$first_day_in_a_week = date("d")-$x;

echo " Date for start week day is: $first_day_in_a_week.$r";

?>

Link to comment
Share on other sites

Here's a function:

 

<?php
function getFirstInWeek ($week_no, $year) {
    $time = strtotime($year . '0104 +' . ($week_no - 1) . ' weeks');
    $monday = strtotime('-' . (date('w', $time) - 1) . ' days', $time);
    return $monday;
}

$week_no = 52;
$year = 2007;
$firstDate = getFirstInWeek($week_no, $year);
echo 'The first date in week ' . $week_no . ' of ' . $year . ' is the ' . date("jS \of F", $firstDate);

/* Prints

The first date in week 52 of 2007 is the 24th of December

*/

 

PhREEEk

 

Link to comment
Share on other sites

was mine any good like to no see no real problam just asking........

 

@redarrow

 

You can save on all the "if" statements by using date('w')

 

if Sunday is first day of week

 

$x = date('w');

 

If Mon is first day

 

$x = (date('w') + 6) % 7;

 

$first_day_in_a_week = date("d")-$x;

 

That will give problems if today is, say, 3rd and $x > 2  (--> zero or negative day !)

 

Better to use (as PHP_Phreeek has) strtotime ("-$x days")

 

or even just

echo date('d-m-y', strtotime("last monday"));

Link to comment
Share on other sites

Ok, From Weekipedia, I got the definition that week starting from 31 st Deecmber 2007 is Week 01 of 2008. This says every thing. Thanx all.

 

Yep... nice thing about strtotime() is that it accepts what one would think is 'out of range' and makes sense of it. There are only 52 weeks per year, so 53 should break the script but it doesn't. You could ask for week 60 of 2007 and it would return 'actual' week 8 of 2008. You could add logic to the function to re-format a bit if out of ranges occur. By the way, the function will receive and calculate negative weeks (backwards) as well. = )

 

Onto what you've mentioned, Monday is considered the first day of a week. If the first Monday is NOT Jan 1st of that year (as it was this current year - 2007), then we have to move backwards to Dec whatever of the year prior until we hit that first Monday. You'll see why then, the initialization calendar date for the function is January 4th of any given year, because we always have to guarantee that there is a January 4th in the first week of every year (according to ISO 8601 standards). Those kinds of things will getchya if you're not careful. = )

 

Glad you found the function useful!

 

PhREEEk

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.