<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anmari &#187; php</title>
	<atom:link href="http://webdesign.anmari.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://webdesign.anmari.com</link>
	<description>Simply effective web services</description>
	<lastBuildDate>Wed, 16 Nov 2011 05:01:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Get the next, or previous day of week from the current date</title>
		<link>http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/</link>
		<comments>http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 20:59:35 +0000</pubDate>
		<dc:creator>anmari</dc:creator>
				<category><![CDATA[AmR Ical Events List]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://webdesign.anmari.com/?p=2000</guid>
		<description><![CDATA[Given a date, get the next or previous Monday, or Tuesday etc from the given date. This is useful when working with ical recurring dates.  If the date given is already that day of week, it is returned as the result. For example to get the following Every 20th Monday of the year, forever: DTSTART;TZID=US-Eastern:19970519T090000 [...]
Related posts:<ol>
<li><a href='http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/' rel='bookmark' title='Calculate date from day of year in php'>Calculate date from day of year in php</a> <small>Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a...</small></li>
<li><a href='http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/' rel='bookmark' title='Day of week for historical dates earlier than 1760'>Day of week for historical dates earlier than 1760</a> <small>Problem with getting the day of week for old dates before 1760? See this....</small></li>
<li><a href='http://webdesign.anmari.com/520/timezones-and-offsets-in-wordpress/' rel='bookmark' title='Timezones and offsets in wordpress'>Timezones and offsets in wordpress</a> <small>Writing a plugin that requires a correct timezone object ?  Not sure how to deal with wordpress installations that may...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Given a date, get the next or previous Monday, or Tuesday etc from the given date.  This is useful when working with ical recurring dates.  If the date given is already that day of week, it is returned as the result.</p>
<p>For example to get the following</p>
<blockquote><p>Every 20th Monday of the year, forever:<br />
DTSTART;TZID=US-Eastern:19970519T090000<br />
RRULE:FREQ=YEARLY;BYDAY=20MO</p>
<p>==&gt; (1997 9:00 AM EDT)May 19<br />
(1998 9:00 AM EDT)May 18<br />
(1999 9:00 AM EDT)May 17</p></blockquote>
<p>One could get the first Monday of the year, the it is fairly easy to get the 20th !</p>
<blockquote>
<pre>&lt;?php

global $amr_day_of_week_no;
$amr_day_of_week_no = array (
 'MO' =&gt; 1,
 'TU' =&gt; 2,
 'WE' =&gt; 3,
 'TH' =&gt; 4,
 'FR' =&gt; 5,
 'SA' =&gt; 6,
 'SU' =&gt; 0
 );    

 function amr_goto_byday ($dateobj, $byday, $sign)    {
 global $amr_day_of_week_no;    
 $dayofweek = $dateobj-&gt;format('w'); /* 0=sunday, 6 = saturday */
 if ($dayofweek == '-1') $dayofweek = <a href="http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/">get_oldweekdays</a>($dateobj); /* php seems to break around 1760   */
$target     = $amr_day_of_week_no[$byday]; /*  mo=1 ,su=7  */
 $adjustment = $target - $dayofweek;
 if ($sign === '+') {
 if ($adjustment &lt; 0) $adjustment = $adjustment + 7;        
 }
 else if ($adjustment &gt; 0) $adjustment = $adjustment-7;    
 $d2 = new DateTime();
 $d2 = clone ($dateobj);         
 date_modify ($d2,$adjustment.' days');    
 return ($d2);
 }    

 /* --------Test data ------------------------- */
$d[] = new DateTime('2009-12-25');
$d[] = new DateTime('2009-12-28');
$d[] = new DateTime('2009-12-29');
$d[] = new DateTime('2009-12-30');
$d[] = new DateTime('2009-12-31');
$d[] = new DateTime('2010-01-01');
$d[] = new DateTime('2010-01-02');
$d[] = new DateTime('2010-01-03');
$d[] = new DateTime('2010-01-04');
$d[] = new DateTime('2010-01-05');
$d[] = new DateTime('2010-01-06');
$d[] = new DateTime('2010-01-07');
$d[] = new DateTime('2010-01-08');
$d[] = new DateTime('2010-01-09');
$d[] = new DateTime('2010-01-10');
$d[] = new DateTime('2010-01-11');

echo '&lt;table&gt;';    
foreach (array ('TU','WE','TH','FR','SA','SU','MO') as $day) {
 echo '&lt;tr&gt;&lt;td&gt; Aiming for '.$day.'&lt;/td&gt;&lt;td&gt;If not this, then next&lt;/td&gt;&lt;td&gt;If not this, then prev&lt;/td&gt;&lt;/tr&gt;';                
 foreach ($d as $i =&gt; $d2)    {
 $d3 = amr_goto_byday ($d2, $day, '+');
 $d4 = amr_goto_byday ($d2, $day, '-');
 $s = $d2-&gt;format('Y m d l');
 echo '&lt;tr&gt;&lt;td&gt; '.$s.'&lt;/td&gt;&lt;td&gt;'.$d3-&gt;format('l,Y m d').'&lt;/td&gt;&lt;td&gt;'.$d4-&gt;format('l,Y m d').'&lt;/td&gt;&lt;/tr&gt;';
 }    
}
echo '&lt;/table&gt;';   
  
?&gt;</pre>
</blockquote>
<p>Related posts:</p><ol>
<li><a href='http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/' rel='bookmark' title='Calculate date from day of year in php'>Calculate date from day of year in php</a> <small>Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a...</small></li>
<li><a href='http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/' rel='bookmark' title='Day of week for historical dates earlier than 1760'>Day of week for historical dates earlier than 1760</a> <small>Problem with getting the day of week for old dates before 1760? See this....</small></li>
<li><a href='http://webdesign.anmari.com/520/timezones-and-offsets-in-wordpress/' rel='bookmark' title='Timezones and offsets in wordpress'>Timezones and offsets in wordpress</a> <small>Writing a plugin that requires a correct timezone object ?  Not sure how to deal with wordpress installations that may...</small></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Day of week for historical dates earlier than 1760</title>
		<link>http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/</link>
		<comments>http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 19:23:54 +0000</pubDate>
		<dc:creator>anmari</dc:creator>
				<category><![CDATA[AmR Ical Events List]]></category>
		<category><![CDATA[ical]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://webdesign.anmari.com/?p=1989</guid>
		<description><![CDATA[Problem with getting the day of week for old dates before 1760? See this.
Related posts:<ol>
<li><a href='http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/' rel='bookmark' title='Get the next, or previous day of week from the current date'>Get the next, or previous day of week from the current date</a> <small>Given a date, get the next or previous Monday, or Tuesday etc from the given date. This is useful when...</small></li>
<li><a href='http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/' rel='bookmark' title='Calculate date from day of year in php'>Calculate date from day of year in php</a> <small>Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a...</small></li>
<li><a href='http://webdesign.anmari.com/404/gift-photo-calendar/' rel='bookmark' title='Gift photo calendar using Google ical files'>Gift photo calendar using Google ical files</a> <small>Use your ical feed calendars (google or other (others by importing into google)) and your own photo's to generate a...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One of the ics files that I encountered in testing my events list plugin came from Google and had a &#8220;Zero Year&#8221; date in it.  There were probably reasons for this and I have to be thankful for the happening -   In looking into it I foundthat the php date format function for day of week $dateobj-&gt;format(&#8216;w&#8217;);  &#8220;breaks&#8221; around the year 1760 and starts returning a &#8220;-1&#8243;.  Since I do have people using my plugin for anniversaries with earlier start dates (It&#8217;s not all musical gigs and sporting events!), I felt I needed to cope with it.</p>
<p>I now use the following code, which appears to work when comparing the weekdays using <a href="http://www.searchforancestors.com/utility/dayofweek.html">http://www.searchforancestors.com/utility/dayofweek.html</a></p>
<pre>$w = $dateobj-&gt;format('w');
if ($w == '-1') $w = get_oldweekdays($dateobj);

function get_oldweekdays ($d) {
   $dummy = new DateTime();
   $dummy = clone ($d);
   date_modify($dummy,'+91500 weeks');
   /* add weeks to get us back to a "working" date -
   a guess from when the date started breaking, plus some extra to be safe */
   $w = $dummy-&gt;format('w');
 return($w);
}</pre>
<p><img class="aligncenter size-full wp-image-1993" title="1700" src="http://webdesign.anmari.com/wp-content/uploads/1700.png" alt="" width="482" height="241" /></p>
<p>Related posts:</p><ol>
<li><a href='http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/' rel='bookmark' title='Get the next, or previous day of week from the current date'>Get the next, or previous day of week from the current date</a> <small>Given a date, get the next or previous Monday, or Tuesday etc from the given date. This is useful when...</small></li>
<li><a href='http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/' rel='bookmark' title='Calculate date from day of year in php'>Calculate date from day of year in php</a> <small>Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a...</small></li>
<li><a href='http://webdesign.anmari.com/404/gift-photo-calendar/' rel='bookmark' title='Gift photo calendar using Google ical files'>Gift photo calendar using Google ical files</a> <small>Use your ical feed calendars (google or other (others by importing into google)) and your own photo's to generate a...</small></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculate date from day of year in php</title>
		<link>http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/</link>
		<comments>http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 02:04:04 +0000</pubDate>
		<dc:creator>anmari</dc:creator>
				<category><![CDATA[AmR Ical Events List]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://webdesign.anmari.com/?p=1956</guid>
		<description><![CDATA[Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a Day in the year (1 to 366), and returns a DateTime Object. /* Year if format YYYY, Day in year 1 to 366 */ function dayofyear2date( $year, $DayInYear ) { $d = new DateTime($year.'-01-01'); date_modify($d, [...]
Related posts:<ol>
<li><a href='http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/' rel='bookmark' title='Get the next, or previous day of week from the current date'>Get the next, or previous day of week from the current date</a> <small>Given a date, get the next or previous Monday, or Tuesday etc from the given date. This is useful when...</small></li>
<li><a href='http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/' rel='bookmark' title='Day of week for historical dates earlier than 1760'>Day of week for historical dates earlier than 1760</a> <small>Problem with getting the day of week for old dates before 1760? See this....</small></li>
<li><a href='http://webdesign.anmari.com/95/comparison-of-event-calendar-plug-ins/' rel='bookmark' title='Comparison of Event Calendar Plug-ins'>Comparison of Event Calendar Plug-ins</a> <small>We are thinking of converting the school&#8217;s website from typo3 to wordpress to make it easier for staff and parents...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Here  is a simple first of several notes on useful date functions.  This function accepts  a year  (YYYY) and a Day in the year (1 to 366), and returns a DateTime Object.</p>
<pre class="php">
<pre> /* Year if format YYYY, Day in year 1 to 366 */</pre>
<p>function dayofyear2date( $year, $DayInYear ) {</p>
<p>$d = new DateTime($year.'-01-01');</p>
<p>date_modify($d, '+'.($DayInYear-1).' days');</p>
<p>return ($d);</p>
<p>}</pre>
<p>I&#8217;m tired of seeing Unix time stamp code examples that break with older dates.  Since my ical calendar scripts can potentially hold recurring dates that are anniversaries and birthdays, my scripts need to cope with dates earlier than 1970, and with timezones and daylight saving,  so I use the DateTime class exclusively now.</p>
<h3>Test Script To Check Function</h3>
<pre class="php">&lt;?php

function dayofyear2date( $year, $DayInYear ) {
 $d = new DateTime($year.'-01-01');
 date_modify($d, '+'.($DayInYear-1).' days');
return ($d);
}

$d[] = new DateTime('1950-12-31');
$d[] = new DateTime('1970-01-01');
$d[] = new DateTime('2010-01-01');
$d[] = new DateTime('2010-12-27');
$d[] = new DateTime('2010-12-31');

foreach ($d as $i =&gt; $d2)    {
 $dayofyear = $d2-&gt;format('z')+1;
 $date = dayofyear2date (
 $d2-&gt;format('Y'),
 $dayofyear ); /* note z returns days from 0 */
 echo '&lt;br /&gt;&lt;b&gt;'.$d2-&gt;format('Y m d')
  .'&lt;/b&gt;&amp;nbsp;&amp;nbsp;'.$dayofyear.'&amp;nbsp;&amp;nbsp;&lt;br /&gt;'
  .$date-&gt;format('Y m d');
}
?&gt;
</pre>
<p>Related posts:</p><ol>
<li><a href='http://webdesign.anmari.com/2000/get-the-next-day-of-week-from-the-current-date/' rel='bookmark' title='Get the next, or previous day of week from the current date'>Get the next, or previous day of week from the current date</a> <small>Given a date, get the next or previous Monday, or Tuesday etc from the given date. This is useful when...</small></li>
<li><a href='http://webdesign.anmari.com/1989/day-of-week-for-dates-earlier-than-1760/' rel='bookmark' title='Day of week for historical dates earlier than 1760'>Day of week for historical dates earlier than 1760</a> <small>Problem with getting the day of week for old dates before 1760? See this....</small></li>
<li><a href='http://webdesign.anmari.com/95/comparison-of-event-calendar-plug-ins/' rel='bookmark' title='Comparison of Event Calendar Plug-ins'>Comparison of Event Calendar Plug-ins</a> <small>We are thinking of converting the school&#8217;s website from typo3 to wordpress to make it easier for staff and parents...</small></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://webdesign.anmari.com/1956/calculate-date-from-day-of-year-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

