<?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>Sean&#039;s Blog &#187; datetime</title>
	<atom:link href="http://seanbehan.com/tag/datetime/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord</title>
		<link>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 21:34:02 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[birthdays]]></category>
		<category><![CDATA[cache counter]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[scopes]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1355</guid>
		<description><![CDATA[There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord" data-url="http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You don&#8217;t want to rely on just the cached day of year because leap year is not accounted for. </p>
<p>The model will need both born_at and birthday columns. </p>
<pre class="wp-code-highlight prettyprint">
create_table :users do |t|
  t.timestamp :born_at  # full timestamp
  t.integer :birthday 	# just the day of year
end
</pre>
<p>The user model also needs a callback (before_save) to set and or update the cached birthday column based on the full timestamp. For convenience, a named scope can be added to the model which will let you call User.birthdays. </p>
<pre class="wp-code-highlight prettyprint">
class User
  # User.birthdays
  scope :birthdays, lambda { where('birthday in(?)', 7.times.map{|i| Time.now.yday + i}) }

  def before_save
  	self.birthday = born_at.yday
  end
end
</pre>
<p>You could also use the week in year (1 &#8211; 52) for the cache. Using the day you can look an arbitrary number of days ahead.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Helper to Remove Leading Zero in 12 Hour Time Format</title>
		<link>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:39:32 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[12 hour time]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[initializers]]></category>
		<category><![CDATA[leading zero]]></category>
		<category><![CDATA[strftime]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=829</guid>
		<description><![CDATA[I can&#8217;t find a strftime() format that will output the hour without the leading zero. For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application. # RAILS_ROOT/config/initializers/datetime.rb Time::DATE_FORMATS[:date] = &#34;%a %b %d, %Y&#34; Time::DATE_FORMATS[:time] = &#34;%I:%M%p&#34; # ... [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frails-helper-to-remove-leading-zero-in-12-hour-time-format%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Rails Helper to Remove Leading Zero in 12 Hour Time Format" data-url="http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>I can&#8217;t find a strftime() format that will output the hour without the leading zero.  For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application.</p>
<pre class="wp-code-highlight prettyprint">
# RAILS_ROOT/config/initializers/datetime.rb
Time::DATE_FORMATS[:date] = &quot;%a %b %d, %Y&quot;
Time::DATE_FORMATS[:time] = &quot;%I:%M%p&quot;
# ...
</pre>
<p>Next I set up a helper function which will string together the :date and :time formats and remove the leading zero in the hour with the .gsub method on the string.</p>
<pre class="wp-code-highlight prettyprint">
  # RAILS_ROOT/app/helpers/application_helper.rb
  def fulltime(created_at)
    created_at.to_s(:date)+&quot; &quot;+created_at.to_s(:time).gsub(/^0/,'').downcase
  end
</pre>
<p>In a view I&#8217;ll just pass the timestamp to the fulltime function</p>
<pre class="wp-code-highlight prettyprint">
fulltime(@post.created_at)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Generate MySQL Datetime Type Using PHP Date() Function</title>
		<link>http://seanbehan.com/php/generate-mysql-datetime-type-using-php-date-function/</link>
		<comments>http://seanbehan.com/php/generate-mysql-datetime-type-using-php-date-function/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 18:22:27 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[strftime]]></category>
		<category><![CDATA[type]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=779</guid>
		<description><![CDATA[If you want to insert a datetime that matches the default mysql datetime type format use this date('Y-m-d H:i:s');]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fphp%2Fgenerate-mysql-datetime-type-using-php-date-function%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Generate MySQL Datetime Type Using PHP Date() Function" data-url="http://seanbehan.com/php/generate-mysql-datetime-type-using-php-date-function/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>If you want to insert a datetime that matches the default mysql datetime type format use this</p>
<pre class="wp-code-highlight prettyprint">
date('Y-m-d H:i:s');
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/php/generate-mysql-datetime-type-using-php-date-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date and Time Helpers in Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 18:16:03 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[time ago in words]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=545</guid>
		<description><![CDATA[Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695 This post was created about &#60;%= time_ago_in_words @post.created_at %&#62; ago]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fdate-and-time-helpers-in-rails%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Date and Time Helpers in Rails" data-url="http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695</p>
<pre class="wp-code-highlight prettyprint">
This post was created about &lt;%= time_ago_in_words @post.created_at %&gt; ago
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem slash Bug in Rails with attr_accessor and Datetime Select Fields</title>
		<link>http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/</link>
		<comments>http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 19:22:15 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[attr_accessor]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[validations]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=492</guid>
		<description><![CDATA[Looks like there is a problem with using the attr_accessor method with datetime form fields http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card validation, I keep getting a &#8216;nil.klass&#8217; error. Sure enough, remove the datetime select from the [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fproblem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Problem slash Bug in Rails with attr_accessor and Datetime Select Fields" data-url="http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Looks like there is a problem with using the attr_accessor method with datetime form fields  http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card validation, I keep getting a &#8216;nil.klass&#8217; error. Sure enough, remove the datetime select from the form and the problem goes away. I need to look into this a little further because I think that this has been resolved.</p>
<p>More information is available here</p>
<p>http://www.ruby-forum.com/topic/130229</p>
<p>and</p>
<p>http://www.google.com/search?q=1+error(s)+on+assignment+of+multiparameter+attributes&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a</p>
<p>The temporary solution is to not use attr_accessor and rather add the field to the database with the correct data type</p>
<pre class="wp-code-highlight prettyprint"> t.datetime :your_datetime_attribute_you_were_once_virtualizing</pre>
<p>. Everything then works as normal. Oh well&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

