D
D
Denis2018-05-05 09:36:17
PHP
Denis, 2018-05-05 09:36:17

How to get the week number of the year in php and mysql?

Hello.
I made a weekly schedule not long ago.
It was necessary to calculate the first week and the final week in the selected period
. I did it like this in PHP

$date_start = strtotime("2018-05-03"); // тут может преобразование отличаться
        $week_start=date("W", $date_start);

That is, we get the 18th week for the date 2018-05-03
We make a request to the database of this type
SELECT `date_invoice` ,WEEK(date_invoice) as week_num FROM `invoce` WHERE `date_invoice` BETWEEN   '2018-05-01' AND '2018-05-31'

And we get 13 records with dates 2018-05-03 and 2018-05-04.
I decided to use the standard WEEK function for the database, as you can see from the query
. But the essence of the problem is that php gives me the date 2018-05-03 - 18 weeks
and MySQL gives the date 2018-05-03 - week 17
But in fact PHP correctly calculates
Here is an example of output from the database, for those who do not believe
5aed50c7922d6165595016.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Vladimirovich, 2018-05-05
@Virtus_pro

try WEEK() to pass second parameter 0 or 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question