I've seen lots of "Date" related questions here, however, I haven't been able to find one that calculates this:
I am trying to figure out the numerical value of a given date string. For example:
$date = "2010-09-01";
Given the date above, how might I be able to determine that this is the "1st" Wednesday of the month.
I know that: date("l", $date); will tell me that it's a Wednesday, but how do I determine if it's the 1st, 2nd, 3rd, or 4th Wednesday of the Month?
Any help on this would be great!
I think this gets you what you want:
$i = (int) (($day_of_month + 6) / 7);
where $day_of_month is from 1 to 31.
You just need a little bit of maths.
If the day of the month is < 8, then it's the first Wednesday. Or Friday. Or Monday. Or Saturday. So if it's between 7 and 15, then it's the second whatever. And so on. #konforce has posted the actual formula.
Related
I have a question and can't find that specific answer.
In my application, my work year start on july 1 every years. So 52 weeks later, I start again on july first. What I want is a field that I enter let say week 32. I want to have the answer of what is the first day of that specified week and show it as date format. So in other words, if I put week 1 in the field, it will give me the result = July 1 and if I put Week 2 in the field, it will gives me july 8, etc. But I think that every year could change. So I need a calculation that will do this. Is it possible in php?
Tks for help
Seby
strtotime("July 1 2013 +".$monthNum." months");
something like this?
As the headline says, PHP's date("W") function gives back the calendar week (for the current day). Unfortunatly it gives back 52 or 53 for the first day(s) of most years. This is, in a simple thinking way, correct, but very annoying, as January 1st of 2012 is not calendar week 52, it's NOT a calendar week of the current year. Most calendars define this as week 0 or week 52 of the previous year.
This is very tricky when you group each day of the year by their calendar week: 1st of January 2012 and 31st of December 2012 are both put into the same calendar week group.
So my question is: Is there a (native) year-sensitive alternative to PHP's date("W") ?
EDIT: I think I wrote the first version of this question in a very unclear way, so this is my edit: I'm searching for a function that gives back the correct calendar week for the first day(s) of the year. PHP's date("W") gives back 52 for the 1st of January 2012, which is "wrong". It should be 0 or null. According to official sources, the first calendar week of a year starts on the first monday of the year. So, if the first day of a year is not a monday, it's not week 1 ! It's week 0. The wikipedia article says
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year.
This becomes tricky as the last days of the year are also in week 52/53. date("W") does not divide into current year and previous year.
This solution converts the excess of december to week 53 and everything in january prior to week 1 to week 0.
$w=(int)date('W');
$m=(int)date('n');
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
echo "week $w in ".date('Y');
2013-12-31 ==> week 53 in 2013
2014-01-01 ==> week 1 in 2014
2015-12-31 ==> week 52 in 2015
2016-01-01 ==> week 0 in 2016
And a small test run, so you can see for yourself ;-)
$id=array(25,26,27,28,29,30,31,1,2,3,4,5,6,7,8);
for($iy=2013;$iy<2067;++$iy){foreach($id as $k=>$v){if($k<7){$im=12;}else{$im=1;}
if($k==7){++$iy;echo '====<br>';}$tme=strtotime("$im/$v/$iy");
echo date('d-m-Y',$tme),' * * ';
//THE ACTUAL CODE =================
$w=(int)date('W',$tme);
$m=(int)date('n',$tme);
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
//THE ACTUAL CODE =================
echo '<b>WEEK: ',$w,' --- ','YEAR: ',date('Y',$tme),'</b><br>';}--$iy;
echo '----------------------------------<br>';}
Is there a (native) year-sensitive alternative to PHP's date("W") ?
No, there isn't.
According to official sources, the first calendar week of a year starts on the first monday of the year.
I'm not sure what official sources you're referring to.
PHP's date("W") returns the week number according to ISO 8601. As an international standard, ISO 8601 counts as one of possibly many "official sources". If its definition of week numbers doesn't fit your application, you're free to use anything else you like.
If you use a non-standard definition of "first week of the year", or if you use an official source that's not widely recognized, expect to have to write your own function to replace date("W"). (I'm pretty sure you'll need to write a function.)
The date 2012-01-01 was a Sunday. ISO 8601, Wikipedia, and php agree that the ISO week number for 2012-01-01 is 52.
ISO 8601 doesn't define a week 0.
So, if the first day of a year is not a monday, it's not week 1 !
Neither ISO nor Wikipedia say that. ISO 8601 defines week number 1 as the week that has the year's first Thursday in it. For 2012, the first Thursday was on Jan 5, so week number 1 was Jan 2 to Jan 8. 2012-01-01 was in the final week of the previous year, in terms of ISO weeks.
If you want something different, you can play with arithmetic, division, and so on. (Try dividing date("z") by 7, for example.) Or you can store that data in a database, and have your weeks any way you like.
If you're dealing with accounting periods, I'd almost certainly store that data in a table in a database. It's pretty easy to generate that kind of data with a spreadsheet.
The text of data in a table is much easier to audit than the text of a php function, no matter how simple that function is. And the data is certain to be the same for any program that accesses it, no matter what language it's written in. (So if your database someday has programs written in 5 different languages accessing it, you don't have to write, test, and maintain 5 different functions to get the week number.)
$d = new DateTime('first monday january '.date('Y'));
echo $d->format("W");
Google brought me here, and I wanted to post the following to help others like me...
I am in the US, and use DayPilot, and it works as follows:
Week starts on Sun, not Mon.
Jan 1st is always Week 1.
If Jan 1st is not a Sunday, Week 1 is less than 7 days.
This all makes a lot of since to me!
Here is my PHP function to copy that behavior:
function ProperWeekNum($inDate)
{$outNum = $inDate->format('W');
//Make week start on Sunday
if ($inDate->format('D') == 'Sun') {$outNum++;}
//Fix begining of year
if (($outNum >= 52) && ($inDate->format('M') == 'Jan')) {$outNum = 1;}
//Fix WEEK #1 is 1-day (Sat)
else //...without this 2022 was off by 1 all year
{$jan1st = new DateTime($inDate->format('Y').'-01-01');
if ($jan1st->format('D') == 'Sat') {$outNum++;}
}
//Return without leading zero
return ltrim($outNum, '0');
}
I use the function as follows, so when I click on DayPilot, my custom popup's Week # always matches DayPilot's Week #:
$weeknum = ProperWeekNum($startdate);
if ($weeknum != ProperWeekNum($enddate))
{$weeknum .= '-'.ProperWeekNum($enddate);}
Probably won't help the OP, but hopefully it helps someone.
Is there a simple way of, given a month and a year, establishing:
How many days there are in that month (factoring in leap years) Done
What day of the week the first day fall upon?
See http://php.net/manual/en/function.cal-days-in-month.php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
and weekdays:
$weekday = date("l", mktime(0,0,0,$month,$day,$year));
$print ($weekday);
The latter is not very efficient but seems nicer than using getdate:
$my_t=getdate(date("U"));
print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");
Output like
Wednesday, September 29, 2011
You may find the answer to your questions with all needed variables and calculations by going to wikipedia. http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
Take a look at the date function, particularly date('t') for the number of days in the month (ie the month given in time()) and date('t',$epoch) for the number of days of the month represented by the timestamp $epoch (which, of course, is given in epoch).
And for the day of the week, there's date('l',$epoch) (where the first argument is a lower-case 'L').
I am trying to create new module for my application where the user can repeat event monthly when they create an event. The problem I am facing is to repeat the event same day of every month. What I wrote works fine. But does not repeat same day of that week of every month. For an example if I create an event 7 July 2011 which is Thursday and If I choose repeat monthly it should create next event on 4 Aug 2011 which is also Thursday. But I could not find a way to repeat at the same day of month. It only works for the same date such as if I choose 7 July, it will create 7th August. I can't think of a way to check the day. Please find my query below.
INSERT INTO EVENT(name, date, usr, repfrequent) VALUES ('test', DATE_ADD('2011-8-7', INTERVAL 1 MONTH), 'Monthly');
The above query repeats the event same date of next month instead of the day such as Thursday of next month. Please let me know if any one knows the answer.
First, IMHO, You'll have to answer the "most important question".
What You mean by monthly?
Sounds maybe stupid, but, I think, You don't want Your events to be repeated monthly.
Scenario1
You want them repeated every 4 weeks.
Why do I think so? Because the WEEKDAY is so important for You, not the DAY OF MONTH, and because this is the fastest solution. If this is the case, don't use months, use 4 weeks as a month interval:
<?php
//
date_default_timezone_set('Europe/Berlin');
//
$startDate = '2012-01-06'; // friday
// Show event dates for next 2 years
// one year = ~52 weeks
// 52 weeks / 4 weeks intervals = 13 months ;)
// So, two years = 26 intervals with 4 weeks
//
for($i = 1; $i <= 26; $i++)
{
$weekOffset = $i * 4;
$nextDate = strtotime("{$startDate} +{$weekOffset} weeks");
echo date('Y-m-d l', $nextDate) . PHP_EOL;
}
?>
Result:
2012-02-03 Friday
2012-03-02 Friday *
2012-03-30 Friday *
2012-04-27 Friday
2012-05-25 Friday
2012-06-22 Friday
2012-07-20 Friday
2012-08-17 Friday
2012-09-14 Friday
2012-10-12 Friday
2012-11-09 Friday
2012-12-07 Friday
2013-01-04 Friday
2013-02-01 Friday
2013-03-01 Friday *
2013-03-29 Friday *
2013-04-26 Friday
2013-05-24 Friday
2013-06-21 Friday
2013-07-19 Friday
2013-08-16 Friday
2013-09-13 Friday
2013-10-11 Friday
2013-11-08 Friday
2013-12-06 Friday
As You can see, there will be situations, when an event will occur twice a month (marked with *).
If You can accept this, do it this way.
Scenario 2
You want them repeated every 1st|2nd|3rd|4th monday|tuesday|wednesday|thursday|friday|saturday of month, and the day of month (1-31) is unimportant.
And that's why I suggest to use the first scenario. Because, what if I create an event on the 5th (!) Sunday of a month? Just take a look in Your calendar, for example January 2012 has 5 sundays, what do You want Your app to do, when I create an "monthly" event on 2012-01-29, it's a sunday, The 5th sunday of January 2012. February 2012 has only four sundays...
That's why I don't think You want your events to be repeated MONTHLY. Wrong naming brings often to a wrong direction ;-)
Threre are a lot more scenarios, but they will need coding, date calculations etc. and my opinion is, You should first answer the "most important question" to Yourself :-)
It would be much easier to repeat every n weeks rather than n months.
I know that you can determine day of the week for any given date using Zeller's Congruence. Hope this helps.
Something I'm trying:
$dow = date('w',mktime(0, 0, 0, date("m",strtotime($date)), date("d",strtotime($date)), date("y",strtotime($date))));
$weeknum = date ("W",mktime(0, 0, 0, date("m",strtotime($date)), date("d",strtotime($date)), date("y",strtotime($date)))) - date("W",strtotime(date("Y-m-01")));
$nextMonthDay = date ("m/d/y",strtotime("$weeknum week $dow", strtotime("2011-09-01")));
You'll need to replace $dow in the last line with Monday->Sunday, which can be done through an associate Array. You can then iterate into the future on the year/month values in the last line, and create rows in your table.
It's not very difficult. If you have a date, calculate which week it is (1-7 day is in first week, 8-14 is 2nd week, etc.)
Then add 28 days. Calculate again. If it falls in the same week, keep it. If not add 7 more days.
You should use PHP to put the days into an array.
Find the number of days in the month: http://php.net/manual/en/function.cal-days-in-month.php
Associate a day to each date using an array, you may also want to associate a date stamp to each instance.
$nextmonth[7]['day']='Thursday';
$nextmonth[8]['day']='Friday';
...
Run through the array and search for the first, second, third of forth instance of the day according to the date specified by the user earlier. For example: 7 July 2011 is the first Thursday of July, find next month's first Thursday in the array.
In PHP, I'm generating a calendar with code from here. I've connected it with Basecamp and am successfully calculating the hours worked each day and sum them for the week and month. To properly calculate overtime, I need the hours from the whole week, even when it spans the end/beginning of a month.
After several hours of trying, I haven't been able to calculate the dates of the days marked with '?' in the above image.
I can't escape the feeling that I'm missing something obvious here, but haven't been able to figure it out. Any help much appreciated, thank you.
You can use mktime() and set a day argument which is less than 1: 0 for the last day, -1 for the day before the last day, etc.
That gives you a UNIX timestamp you can use to find out the weekday and then - until you've reached Sunday/Monday (whatever week start you use) - subtract -86400 or call mktime() again with the day reduced by 1.
Example:
mktime(0, 0, 0, 12, -1, 2010); # timestamp of Nov 29th 00:00:00 2010