I have a script that process raw CSV data and generate report grouped by week of the year.
Which looks something like this:
//timezone is set to Europe/London
$date = new DateTime($raw['order_date']); // example: 12/31/2012
$key = $date->format('Y W'); // 2012 01
$array[$key][] = $raw['product_id'];
Everything was working fine until I tried to parse data generated over new year eve, for some reason system believes that 31 December 2012 is week number 1 of year 2012.
I'm not sure is it bug or a feature, but produced reports are definitely wrong.
What is a correct way of passing this issue and keeping data grouped by weeks?
Try this:
$key = $date->format('o W');
I think the week is defined Thursday, meaning if the Thursday falls in 2013, then it's counted as being a week of that year.
$w=(int)date('W');
$m=(int)date('n');
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
$year = date('Y');
if($w==53)
{
$year=$year+1;
}
past year i am getting all d records properly but in 2016 april first select than I got april 2015 report show any thing missing or its leap year problem
Related
I'm working on a PHP function which calculates holidays:
function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)
The holidays are returned as timestamps in an array.
Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year") and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.
This bug is actually mentioned in the change log:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
But I'm using PHP 5.3.8.
Why am I experiencing this error?
Looks like you are just missing an "of":
echo date('Y-m-d', strtotime('first monday of february 2010'));
will give the expected result. See the PHP Manual on Relative dates for the various input formats.
Depending on your version of PHP the 'of' statement may or may not work. As another solution try:
echo date('Y-m-d',strtotime('monday February 2010'));
will return the first monday of February 2010. Works for all days as well.
I am using a date() format to return the starting weekday of a month. The code I have below is how I am attempting to achieve this. For the current year (2018) this works as normal. For example This month is august and the starting weekday is a Wednesday so it will return a 3 for Wednesday. (It works so far)
As we advance the year to 2019 it starts to get the starting weekday wrong.
For example January 2019 starts on a Tuesday so it should return 2 but returns 1. (one day out)
This error seems to be cumulative so if we go to 2020 then it is 2 days out etc.
I have tried so hard to format this Date() correctly but to no avail. Is this even the correct way to do this?
Code:
$future_month = 5 /*for January 2019*/
$starting_weekday = date('N',mktime(0, 0, 0, date('m', strtotime('+'.$future_month.' months', strtotime(date('Y-m-01')))), 1));
Many Thanks
Cameron
Your code makes this much more complicated than it needs to be.
$dt = new DateTime('first day of +5 months')
$dt->format('N'); // "2"
Why does this sum only 4 days when using 25-Oct-13 and 5 days (as expected) when using other dates?
<?php
echo date('d-M-y',(strtotime('25-Oct-13') + (432000)));
?>
This depends on your timezone. The last Saturday of October is the end of Daylight Saving Time (DST) in some locales. Therefore the night of October 26th to 27th in 2013 may or may not contain an extra hour.
Circumvent this issue by adding actual days instead of hours:
$myDate = new \Datetime('2013-10-25');
$myDate->add(new \DateInterval('P5D'));
This does return Oct 30th 2013.
More strange stuff can happen from incorrectly assuming that days are always exactly 24 hours.
$date = "2014-09-17";
echo date('Y-m-d', strtotime($date.' + 5 days'));
I ask user to enter start and end date in a certain application. Then I calculate the no. of weeks in between these two dates.
I want to fetch documents created per week in the given period of time.
I am unable to implement a logic which will help me to fetch first week, second week, third week, and so on.
I will use this as input to database and then create graph. I am using php as the server side language. Anyone could suggest me the Algorithm.
ex:
Start date: 20/07/2012
End Date: 19/02/2013.
Total no. of days: 214.
Total no. of weeks: 30 (30 weeks and 4 days)
I want to fetch period of 20/07/2012 to 27/07/2012 and no. of Docs during this period. Till 15/02/2013.
Without seeing your database structure here is a sample of what you require
SELECT YEAR(doc_created) AS report_year, WEEKOFYEAR(doc_created) AS report_week, COUNT(*) AS doc_count
FROM documents
WHERE doc_created BETWEEN '2012-07-20' AND '2013-07-19'
GROUP BY YEAR(doc_created), WEEKOFYEAR(doc_created)
grouping by weekofyear is not enough, as period may overlap the next year, so we should also include the year.
possible results:
year week docs
2012 1 4
2012 3 1
2013 1 65
2013 2 4
as you can see 2012 week 1 would overlap 2013 week 1, if we just used week number.
for your grid/graph use the reference (year wk) and amount (docs)
e.g. 2012wk1 is 4, 2012wk3 is 1
Try this code:
$start_date = "20-07-2012";
$end_date = "20-08-2012 ";
while($start_date){
if(strtotime($start_date) > strtotime($end_date)){
exit;
}
$start_date = strtotime("+7 day", strtotime($start_date));
$start_date = date("d-m-Y", $start_date);
echo $start_date;
echo "<br/>";
}
I am having a problem with php's DateTime functions.
Today is monday 3 december.
Assuming the following code:
$dte = new DateTime(date('Y-m-d H:i:s'));
var_dump($dte->format('Y-W'));
$dte->modify('+4 weeks');
var_dump($dte->format('Y-m-d H:i:s -- Y_W'));
$dte->modify('+1 days');
var_dump($dte->format('Y-m-d H:i:s -- Y_W'));
After four weeks it would be 31st of december. I would suspect to get the last week of the year (52?). But what I get is week 1 of 2012 as you can see in the following output.
string '2012-49' (length=7)
string '2012-12-31 14:48:00 -- 2012_01' (length=30)
string '2013-01-01 14:48:00 -- 2013_01' (length=30)
So my problem is that after the first modification I think I should get:
2012-12-31 14:48:00 -- 2012_52
but instead I get
2012-12-31 14:48:00 -- 2012_01
So why does the week go back to 01 without incrementing the year, and than why does the other line gives me 2013_01 ?
EDIT::
I now see that the week before is week 52, anything to do with leap year?
But then again, how can the week go back to 01 without incrementing the year?
So why does the week go back to 01 without incrementing the year, and than why does the other line gives me 2013_01 ?
I think you're displaying the "year" instead of the "week-year". When you're using week numbers, it's the week-year that's the relevant part; simple "year" is only relevant with respect to month and day.
EDIT: I think you want the o format specifier instead, so try:
var_dump($dte->format('Y-m-d H:i:s -- o_W'));
That should show you 2013_01 for December 31st 2012, as it's in week 1 of week-year 2013.
So basically, I don't think this is a bug in DateTime - it's just a misunderstanding of how "week of year" is meant to be used.
This seems to be no bug. According to the documentation W will return ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) and because Mon, 31 Dec 2012 15:04:46 +0100 is Monday it will be 1 instead of 52.
Further information on Wikipedia and this nice site.