I am just facing issue with getting right week for my orders which I want put the numbers of orders in different weeks it into graph.
The issue is that the order made in 2022-01-02 is week 52.. but order made in 2022-01-03 is week 1.. how can i achieve that the both orders will be in the same week as it is January, when every year week ends in different date in January? I hope you got my point.
PHP Example:
$date = "2022-01-02 00:00:00";
$week = (float) date("W", strtotime($date));
print $week . "\n";
$date = "2022-01-03 00:00:00";
$week = (float) date("W", strtotime($date));
print $week;
My Answer:
One way to achieve this is to use the ISO-8601 week date system, which defines the first week of the year as the week that contains the first Thursday of the year. This system ensures that the week numbering is consistent across different years.
Here's an example of how you can calculate the week number using the ISO-8601 week date system in PHP:
php
$date = "2022-01-02 00:00:00";
$week = (float) date("W", strtotime($date . " +3 days")); // Adding 3 days to shift the week to ISO-8601
print $week . "\n";
$date = "2022-01-03 00:00:00";
$week = (float) date("W", strtotime($date . " +3 days")); // Adding 3 days to shift the week to ISO-8601
print $week;
By adding 3 days to the date before calculating the week number, you are effectively shifting the week numbering to align with the ISO-8601 week date system. This way, both orders made on January 2nd and January 3rd will be in the same week (week 53 in this case).
Rate this post
3 of 5 based on 6372 votesComments