I need to filter some results of this week and next week. But it gives me no result for this week.
I have records with the date "2013-10-25 12:00:00" and "2013-10-22 12:00:00".
I am getting current week by date('W', time()) in php and using WEEK(tdi.due_at) in mysql query.
date('W', time()) return 43 which is current week number.
but WEEK(tdi.due_at) returns 42 why?
MySQL WEEK() has also an optional mode parameter that sets the condition when the first week of a year starts.
Why not you subtract -1 from PHP date('W') - 1.
or add +1 to MySQL WEEK(tdi.due_at) + 1.
PHP Indexed with 1 and MySQL starts index with 0 like JavaScript does.
Related
I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this?
I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011.
strtotime can do this out of the box using the {YYYY}W{WW} format:
echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03
Note that the week number needs to be two digits.
Shamefully, DateTime::createFromFormat, the fancy new PHP 5 way of dealing with dates, seems unable to parse this kind of information - it doesn't have a "week" placeholder.
$week: The week number
$year: The year number
Then:
$timestamp = gmmktime (0, 0 , 0 , 1, , 4 + 7*($week - 1), $year);
The 4 + 7*($week - 1) comes from the fact that according to ISO 8601, the first week of the year is the one that contains January 4th.
strtotime('1/1/2011 + 4 weeks') (1/1 ist always in week number one; this would bring me to week number five). if you want any timestamp in the week then that's all you need, else you would have to go to the monday in this week:
$t = strtotime('1/1/2011 + 4 weeks');
$t -= 24 * 60 * 60 * date('w', $t);
Update: Instead of 1/1/2011 use the first monday in 2011. The 2nd calculation is not needed anymore.
I don't understand why PHP and MySQL give two different values of month number for some particular date:
echo date('W', strtotime('2014-04-16'));
this gives 16
while this:
SELECT WEEK('2014-04-16')
gives 15
Could anyone help me?
Thanks
Try using SELECT WEEK('2014-04-16',3) for returning a PHP compatible value. The ,3 indicates that this has weeks starting on Mondays and weeks numbered 1-53.
Play with MySQL WEEK() function parameters: see here
For the same result you should use mode parameter in MYSQL function with value of 4.
PHP
string date ( string $format [, int $timestamp = time() ] )
W ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
MYSQL
WEEK(date[,mode])
For mode values with a meaning of “with 4 or more days this year,” weeks are numbered according to ISO 8601:1988:
Can someone kindly explain me why these two give different results?
I execute this with PHP.
date("YW",mktime(0, 0, 0, 3, 22 , 2013)); // outputs 201312
And when I execute this with MySQL
SELECT YEARWEEK(now()); // outputs 201311
You need to specify mode 3 on the mysql YEARWEEK call:
SELECT YEARWEEK(now(),3);
The PHP date() placeholder W returns the week number according to the ISO 8601 specification. That means weeks start on Monday (not Sunday), the first week of the year is number 1 (not 0), and that week is the first one that with more than half its days in the new year (so it has to be January by Thursday). According to the documentation for the MySQL WEEK function, that combination of options is mode 3.
Also, to pull Alles's note into the accepted answer because it's important: the placeholders Y and W don't go together. If you want the year that goes with the ISO week number, you should use o instead of Y. For example, consider the week starting on Monday, December 29th, 2014:
date('YW', mktime(0,0,0,12,29,2014)); #=> 201401 : 1st week of 2014??
date('oW', mktime(0,0,0,12,29,2014)); #=> 201501 : better
Please be aware that YEARWEEK('2012-01-01', 3) => 201152 while the PHP "YW" will give 201252. The year in the result may be different from the year in the date argument for the first and the last week of the year. (i.e. the year in the YEARWEEK is the year of the Monday of the week and not the year of the date being used to calculate).
In order to get the right result, you need to do
date("oW",mktime(0, 0, 0, 1, 1, 2012)); // outputs 201152
as "o" gives you the Year the week belongs to.
I hope this helps.
YEARWEEK takes a second (optional) parameter that specifies the range of the week [0- 53] or [1-53]).
This function returns the week number for date. The two-argument form
of WEEK() enables you to specify whether the week starts on Sunday or
Monday and whether the return value should be in the range from 0 to
53 or from 1 to 53. If the mode argument is omitted, the value of the
default_week_format system variable is used.
while date(W) is an ISO8601 date that is always in the range [01-53]. Therefore my guess is that by default YEARWEEK is using the [0-53] range.
So, if you want to get the same result try using 1 as the second parameter for YEARWEEK
I have the following PHP datetime object giving strange results:
<?php
$date = new DateTime("2013-01-01");
$date2 = new DateTime("2011-01-01");
$interval = $date2->diff($date);
echo $interval->m;
?>
When using months (m), returns 0. Incorrect.
When I switch the interval to years (y) it returns 2 which is correct.
When I switch to days (d) it returns 0, incorrect.
When I switch to days using "days", it returns 731 which is correct
I am not sure why certain intervals are working and others are not. Any ideas or is this expected? If possible - I would like to continue using DateTime to find this difference but an open to other necessary means.
See, $interval is an object, not some primitive value. In your example this interval consists of two years, zero months and zero days. It doesn't get automatically converted into 'interval in months, interval in days' etc. when you're querying its properties: it just returns their values. And it's quite right: should you consider 29 days interval a month interval, for example?
The only exception is $days property (not $d!), which actually has a calculated value of days in that interval. And it's quite well described in the documentation:
$days
Total number of days between the starting and ending dates in a
DateTime::diff() calculation
How can I calculate the number of Sundays that have already passed from the current YEAR till today. I would also like to calculate the number of Sundays that have already passed from the current MONTH till today.
eg
today is 14 April 2012 I should get 2 for the Number of Sundays
that are passed from the current month.
Can anyone give me a hint or a tutorial how this can be achieved ?
well i guess it is simple enough using the date() function
//will give you the amount of sundays from the begining of the year
$daysTotal = ceil((date("z") - date("w")) / 7);
//will give you the amount of sundays from the begining of the month
$daysTotal = ceil((date("j") - date("w")) / 7);
I didn't test it, you might want to check if the round() function works right in this situation but the point is passed i think
good luck
Date('W') might help you. Check php manual for the format - http://php.net/manual/en/function.date.php .
Do not forget according to manual - ISO-8601 week number of year, weeks starting on Monday.
So this week will be counted even if this not Sunday yet. So in that case reduce the number by 1.
function get_sunday_since_year_start($today=null)
{
if($today==null) $today = time();
if(date('D',$today)=='Sun')
return Date('W',$today);
else
return Date('W',$today)-1;
}
function get_sunday_since_month_start()
{
return get_sunday_since_year_start()-get_sunday_since_year_start(strtotime(date('Y-m-01 00:00:00')));
}