Increment week number from one year to one year

I can not write many word to describle my problem so I post code here and say what I want, please help me get the result
I have an input:

$arr = [
	['id' => '1', 'dt' => '2017-12-30', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '2', 'dt' => '2017-12-31', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '3', 'dt' => '2018-01-01', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '4', 'dt' => '2018-01-02', 'week' => '', 'real_week' => '', 'date_name' => ''],
];

week key in array will increment follow 1, 2, 3, 4 … with days are the same week, I try

$arr = [
	['id' => '1', 'dt' => '2017-12-30', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '2', 'dt' => '2017-12-31', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '3', 'dt' => '2018-01-01', 'week' => '', 'real_week' => '', 'date_name' => ''],
	['id' => '4', 'dt' => '2018-01-02', 'week' => '', 'real_week' => '', 'date_name' => ''],
];

$first_wk = new DateTime($arr[0]['dt']);
$first_wk = $first_wk->format('W');
foreach ($arr as $i => $item)
{
	$dt = new DateTime($item['dt']);
	$arr[$i]['real_week'] = $dt->format('W');
	$arr[$i]['week'] = $dt->format('W') - $first_wk + 1;
}

but I get wrong result

$arr = [
	['id' => '1', 'dt' => '2017-12-30', 'week' => '1', 'real_week' => '', 'date_name' => ''],
	['id' => '2', 'dt' => '2017-12-31', 'week' => '1', 'real_week' => '', 'date_name' => ''],
	['id' => '3', 'dt' => '2018-01-01', 'week' => '-50', 'real_week' => '', 'date_name' => ''],
	['id' => '4', 'dt' => '2018-01-02', 'week' => '-50', 'real_week' => '', 'date_name' => ''],
];

the result I want to get

$arr = [
	['id' => '1', 'dt' => '2017-12-30', 'week' => '1', 'real_week' => '', 'date_name' => ''],
	['id' => '2', 'dt' => '2017-12-31', 'week' => '1', 'real_week' => '', 'date_name' => ''],
	['id' => '3', 'dt' => '2018-01-01', 'week' => '2', 'real_week' => '', 'date_name' => ''],
	['id' => '4', 'dt' => '2018-01-02', 'week' => '2', 'real_week' => '', 'date_name' => ''],
];

when the years is the same it’s oke but different years it’s wrong, please help me!

I don’t get the mission. On which condition should the week increment?

The way I read it is that the week number is based on the very first date being in week one. Possibly calculate what the first day of that first week would be (Sunday / Monday), then just calculate number of days since then, and get a pseudo-week number from there?

PHP will do that for you so you dont need do this. Remember that 1 year has 52 weeks. If you run

$dt = new DateTime('2018-01-01');
echo $dt->format('W');

What do you get? it’s 1, first week number in year. Do the same with
2018-01-02

to

$dt = new DateTime('2018-12-31');
echo $dt->format('W');

You will get 52. And then you run

$dt = new DateTime('2019-01-01');
echo $dt->format('W');

You will get 1 but I dont like this, I want its increment to 53 instead 1. It’s called real_week in my array input, I think this explain you will get it.

It’s increment follow real_week. Remember that 1 year has 52 weeks. If you run

$dt = new DateTime('2018-01-01');
echo $dt->format('W');

What do you get? it’s 1, first week number in year. Do the same with
2018-01-02

to

$dt = new DateTime('2018-12-31');
echo $dt->format('W');

You will get 52. And then you run

$dt = new DateTime('2019-01-01');
echo $dt->format('W');

You will get 1 but I dont like this, I want its increment to 53 instead 1. It’s called real_week in my array input, I think this explain you will get it.

@droopsnoot, @chorn after add more explain I found my solution, thank for yours support

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.