<?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>Web Cash &#187; date</title>
	<atom:link href="http://www.earn-web-cash.com/tag/date/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Create a Random Date in PHP</title>
		<link>http://www.earn-web-cash.com/2008/02/07/how-to-create-a-random-date-in-php/</link>
		<comments>http://www.earn-web-cash.com/2008/02/07/how-to-create-a-random-date-in-php/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 03:18:06 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/07/how-to-create-a-random-date-in-php/</guid>
		<description><![CDATA[While browsing some forums the other day, I came across this question &#8211; &#8220;How do I find a random date in PHP?&#8221; This is a pretty simple operation, if we make use of a few built-in PHP functions &#8211; time(), strtotime(), and date(). In this article, we&#8217;ll see how these three can work together to [...]]]></description>
			<content:encoded><![CDATA[<p>While browsing some forums the other day, I came across this question &#8211; &#8220;How do I find a random date in PHP?&#8221;</p>
<p>This is a pretty simple operation, if we make use of a few built-in PHP functions &#8211; <code>time()</code>, <code>strtotime()</code>, and <code>date()</code>.  In this article, we&#8217;ll see how these three can work together to find a random date within a given range.<br />
<span id="more-102"></span></p>
<h5>The PHP time() Function and Timestamps</h5>
<p>The PHP <code>time()</code> function is very simple.  It returns the current time.</p>
<p>It helps us understand something else though &#8211; Unix timestamps.  The <code>time()</code> function, along with all of PHP&#8217;s time/date functions, deals with Unix timestamps.  This is an integer which represents the number of seconds that have elapsed since Jan 1, 1970 (considered the beginning of the &#8220;Unix epoch&#8221;).</p>
<p>For example, the current timestamp for Thursday, Feb. 7 9:57 PM is 1202439456.  You can do the math if you want.  I&#8217;ll trust PHP.</p>
<p>This allows us to do a lot of simple operations with time, however.  We know that a timestamp deals with seconds &#8211; so we can add/subtract a certain number of seconds to modify the time by minutes, hours, or days.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  Current time</span>
<span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  One minute ago</span>
<span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  Two minutes in the future</span>
<span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  One hour ago</span></pre></div></div>

<h5>PHP&#8217;s date() Function &#8211; Formatted Dates</h5>
<p>The <code>time()</code> function and timestamps become really powerful when you understand how to use the <code>date()</code> function.</p>
<p>This takes two parameters &#8211; a date format string and a timestamp.  It then takes the timestamp and outputs the given time in the given format.  You can find a list of date format characters at <a href="http://us2.php.net/manual/en/function.date.php">php.net</a>.  Here are a few examples, though&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m-d-Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  02-07-2008</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;M d Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Feb 07 2008</span>
<span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F d, Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// February 07, 2008</span></pre></div></div>

<p>Notice that the letters (mMFdY) represent the actual dates, while the spacing and punctuation is just used for formatting.</p>
<h5>Getting Custom Timestamps with strtotime()</h5>
<p>The last function to look at is <code>strtotime()</code>.  This takes a formatted date string (like &#8220;Feb 07 2008&#8243; or &#8220;1 day ago&#8221;) and returns the appropriate Unix timestamp.</p>
<p>This allows you to fetch a timestamp for pretty much any date you want and then manipulate it in other ways &#8211; using the date() function or your own mathematical functions.</p>
<p>This is also going to help us find a random date in between two given dates.</p>
<h5>Finding a Random Date</h5>
<p>In order to find a random date, we need three things.  The <code>rand()</code> function &#8211; to get a random number or timestamp.  A start date.  An end date.</p>
<p>In this simple example, we&#8217;ll find a random time between the start of the Unix epoch (timestamp = 0) and the current time.  Then we&#8217;ll format it in the m-d-Y format for easy readability.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m-d-Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Run that, and you&#8217;ll find a random date between &#8220;Jan 1 1970&#8243; and whatever your current date is &#8211; &#8220;Feb 7 2008&#8243; for me.</p>
<p>We can make this a little better by using <code>strtotime</code> to set the min and max value for <code>rand()</code>.  In this example, we&#8217;ll find a date in 2007 &#8211; between &#8220;Jan 01 2007&#8243; and &#8220;Dec 31 2007&#8243;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jan 01 2007&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dec 31 2007&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m-d-Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And there you go.  Replace the <code>strtotime()</code> calls that I used, and you can find a random date within any given daterange.</p>
<p>However, take note that there are limitations to the extent of the Unix timestamp.  By using a negative number, the earliest date you can get with <code>date()</code> is around 1901.  The latest date you&#8217;ll be able to work with is around 2025.  This is a limitation based on the size of integers in PHP &#8211; the Unix timestamp just can&#8217;t get any bigger in its basic form.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/07/how-to-create-a-random-date-in-php/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

