Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #2042 > unrolled thread
| Started by | Amit Pawar <amit.pawar@pragmites.com> |
|---|---|
| First post | 2011-06-09 05:25 -0700 |
| Last post | 2011-06-10 10:41 +1000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.php
How To Find All Weeks First date and Last date? Amit Pawar <amit.pawar@pragmites.com> - 2011-06-09 05:25 -0700
Re: How To Find All Weeks First date and Last date? Luuk <Luuk@invalid.lan> - 2011-06-09 21:03 +0200
Re: How To Find All Weeks First date and Last date? Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-09 20:03 -0400
Re: How To Find All Weeks First date and Last date? Jeff North <jnorthau@yahoo.com.au> - 2011-06-10 10:41 +1000
| From | Amit Pawar <amit.pawar@pragmites.com> |
|---|---|
| Date | 2011-06-09 05:25 -0700 |
| Subject | How To Find All Weeks First date and Last date? |
| Message-ID | <a8d7c9f3-aab5-446e-829f-809b18463bbb@e17g2000prj.googlegroups.com> |
Hi, I want to find All no of week in any month. And first and last date of that week. Assume Month is Jan 2011 date in Y-m-d 1st week -> Start date=2011-01-01 End date=2011-01-01 2nd week -> Start date=2011-01-02 End date=2011-01-08 3rd week -> Start date=2011-01-09 End date=2011-01-15 4th week -> Start date=2011-01-16 End date=2011-01-22 5th week -> Start date=2011-01-23 End date=2011-01-29 6th week -> Start date=2011-01-30 End date=2011-01-31
[toc] | [next] | [standalone]
| From | Luuk <Luuk@invalid.lan> |
|---|---|
| Date | 2011-06-09 21:03 +0200 |
| Message-ID | <4df1190e$0$49045$e4fe514c@news.xs4all.nl> |
| In reply to | #2042 |
On 09-06-2011 14:25, Amit Pawar wrote:
> Hi,
>
> I want to find All no of week in any month.
> And first and last date of that week.
>
> Assume Month is Jan 2011
>
> date in Y-m-d
> 1st week -> Start date=2011-01-01 End date=2011-01-01
> 2nd week -> Start date=2011-01-02 End date=2011-01-08
> 3rd week -> Start date=2011-01-09 End date=2011-01-15
> 4th week -> Start date=2011-01-16 End date=2011-01-22
> 5th week -> Start date=2011-01-23 End date=2011-01-29
> 6th week -> Start date=2011-01-30 End date=2011-01-31
$d=mktime (0,0,0,1,1,2011);
for ($i=0; $i<35; $i++) {
$x=$d+($i*24*3600);
if (date("w",$x)==0) {
print date("W d-m", $x).date(" d-m\n", $x+(6*24*3600));
}
}
You need to change this in order to get it to work ;)
--
Luuk
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-06-09 20:03 -0400 |
| Message-ID | <isrn11$sg8$1@dont-email.me> |
| In reply to | #2042 |
On 6/9/2011 8:25 AM, Amit Pawar wrote: > Hi, > > I want to find All no of week in any month. > And first and last date of that week. > > Assume Month is Jan 2011 > > date in Y-m-d > 1st week -> Start date=2011-01-01 End date=2011-01-01 > 2nd week -> Start date=2011-01-02 End date=2011-01-08 > 3rd week -> Start date=2011-01-09 End date=2011-01-15 > 4th week -> Start date=2011-01-16 End date=2011-01-22 > 5th week -> Start date=2011-01-23 End date=2011-01-29 > 6th week -> Start date=2011-01-30 End date=2011-01-31 First of all, what day does your week start on? Sunday, from the looks of it, but need to be sure. Next, most calendars would consider that since Jan 1 is on a Saturday, that is part of the last week of 2010, and the first week of 2010 would start on Jan 2. So first you need to find the first Sunday. This is pretty easy to do by getting the day of week for 2011-01-01 as a number (1-7 for Monday-Sunday). See date() for DateTime::format() with the 'N' format. From there it's a simple matter to get the end of the week by adding 6 days (DateInteral object). You can then get the start of the next week by either adding 1 day to the end of the week or 7 days to the start of the previous week. Not too hard if you look up the DateTime object in the manual. It contains all the references you need. See http://us.php.net/manual/en/class.datetime.php. (Note: adding 60*60*24*7 seconds will be off by one hour for 1/2 the year if you run Daylight Savings Time in the summer - not necessarily a problem if you only are interested in dates). -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Jeff North <jnorthau@yahoo.com.au> |
|---|---|
| Date | 2011-06-10 10:41 +1000 |
| Message-ID | <ahp2v691mkmbvi1gv7msf2mql7fheejjie@4ax.com> |
| In reply to | #2062 |
On Thu, 09 Jun 2011 20:03:30 -0400, in comp.lang.php Jerry Stuckle
<jstucklex@attglobal.net>
<isrn11$sg8$1@dont-email.me> wrote:
>| On 6/9/2011 8:25 AM, Amit Pawar wrote:
>| > Hi,
>| >
>| > I want to find All no of week in any month.
>| > And first and last date of that week.
>| >
>| > Assume Month is Jan 2011
>| >
>| > date in Y-m-d
>| > 1st week -> Start date=2011-01-01 End date=2011-01-01
>| > 2nd week -> Start date=2011-01-02 End date=2011-01-08
>| > 3rd week -> Start date=2011-01-09 End date=2011-01-15
>| > 4th week -> Start date=2011-01-16 End date=2011-01-22
>| > 5th week -> Start date=2011-01-23 End date=2011-01-29
>| > 6th week -> Start date=2011-01-30 End date=2011-01-31
>|
>| First of all, what day does your week start on? Sunday, from the looks
>| of it, but need to be sure.
>|
>| Next, most calendars would consider that since Jan 1 is on a Saturday,
>| that is part of the last week of 2010, and the first week of 2010 would
>| start on Jan 2.
>|
>| So first you need to find the first Sunday. This is pretty easy to do by
>| getting the day of week for 2011-01-01 as a number (1-7 for
>| Monday-Sunday). See date() for DateTime::format() with the 'N' format.
>|
>| From there it's a simple matter to get the end of the week by adding 6
>| days (DateInteral object). You can then get the start of the next week
>| by either adding 1 day to the end of the week or 7 days to the start
>| of the previous week.
>|
>| Not too hard if you look up the DateTime object in the manual. It
>| contains all the references you need. See
>| http://us.php.net/manual/en/class.datetime.php.
>|
>| (Note: adding 60*60*24*7 seconds will be off by one hour for 1/2 the
>| year if you run Daylight Savings Time in the summer - not necessarily a
>| problem if you only are interested in dates).
Just to add to Jerry's post:
The problem of Daylight Saving Time can be corrected by making sure
that the time is set to midday not midnight i.e.
<?php
$Aday = 60 * 60 * 24;
$dt = strtotime('2011-01-01 12:00:00');
for( $x=0; $x<53; $x++ ) {
echo date ( "W w Y-m-d", $dt )."<br>";
$dt += $Aday * 7;
}
?>
Change the 12:00:00 to 00:00:00 to see what effect the DST has on the
calculations.
The interesting part is that the date() drops a whole day when DST
starts and picks up the correct date when DST ends.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web