11

I want to set the first day of the week to Thursday (not Sunday or Monday), because it's the company's cut-off date.

I already have a code to determine the current week number of a date but it starts in Sunday or Monday.

How to modify these to my preference?

function findweek($date) {
    $monthstart=date("N",strtotime(date("n/l/Y",strtotime($date))));
    $newdate=(date("j",strtotime($date))+$monthstart)/7;
    $ddate=floor($newdate);
    if($ddate != $date) {
        $ddate++;
    }
    return $ddate;
}
Donnelle
  • 5,689
  • 3
  • 27
  • 31
Rayden Black
  • 135
  • 1
  • 12
  • I am getting ur point.......... wat exactly you want to do – Vikas Umrao Sep 15 '15 at 03:39
  • in our company the start of week count for payroll is thursday not sunday or monday so iwant to start the count of every week of every month is thursday. – Rayden Black Sep 15 '15 at 03:41
  • @RaydenBlack see if this can help you http://codepad.org/tI0nMQPo you may need to adjust datetime format – Shehary Sep 15 '15 at 04:09
  • I checked it but I don't if it will help because it only display the output thursday not to start the week count on every thursday. I will check if the link you given will add to the solution. – Rayden Black Sep 15 '15 at 05:09
  • 1
    possible duplicate of [Changing the start day of week from 'monday' to 'tuesday' in Php](http://stackoverflow.com/questions/10174745/changing-the-start-day-of-week-from-monday-to-tuesday-in-php) – Avalanche Sep 15 '15 at 06:46
  • @RaydenBlack, in question you mentioned that `but it starts in sunday or monday` so i code something where starts from `thursday,` see if you can add it to the function and get desired return – Shehary Sep 15 '15 at 07:19
  • I saw from google search this kind of php function intlcalendar.setfirstdayofweek, how can i use this? – Rayden Black Sep 16 '15 at 02:01
  • 2
    May I ask how exactly are you planning to use this "start of the week" setting? Is it do display a calendar? Do some internal calculations? Otherwise, if you want `date('N')` to return 1 for Thursday, you can just subtract 3 or 4 from it (depending on whether the week starts on Monday or Sunday). – Sergey Vidusov Feb 16 '16 at 10:35
  • I was ask to create an application that calculates the work hours of the employees that starts from Thursday and ends in Wednesday weekly. – Rayden Black Feb 19 '16 at 08:55

2 Answers2

2

http://php.net/manual/en/datetime.formats.relative.php says that as of PHP version 5.6.23, 7.0.8 "Weeks always start on monday. Formerly, sunday would also be considered to start a week." That said, is your problem that the number of weeks returned might be incorrect depending on whether today falls on or before Thursday of the current week? Maybe try something like this:

$date = new DateTime();
$week = intval($date->format('W'));
$day = intval($date->format('N'));
echo $day < 4 ? $week-1 : $week;

If subtracting 1 isn't the answer you could play around with addition/subtraction, comparing the result with the actual answer you know to be true until you get the right formula. Hope this helps!

robbymarston
  • 344
  • 3
  • 16
0

This should work.

function findweek($date, $type = "l") {
    $time = strtotime($date);
    return date($type, mktime(0, 0, 0, date("m", $time) , date("d", $time)-date("d", $time)+1, date("Y", $time)));
}

echo findweek('2015-09-16');
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48