How to Create a Random Date in PHP

While browsing some forums the other day, I came across this question - “How do I find a random date in PHP?”

This is a pretty simple operation, if we make use of a few built-in PHP functions - time(), strtotime(), and date(). In this article, we’ll see how these three can work together to find a random date within a given range.

The PHP time() Function and Timestamps

The PHP time() function is very simple. It returns the current time.

It helps us understand something else though - Unix timestamps. The time() function, along with all of PHP’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 “Unix epoch”).

For example, the current timestamp for Thursday, Feb. 7 9:57 PM is 1202439456. You can do the math if you want. I’ll trust PHP.

This allows us to do a lot of simple operations with time, however. We know that a timestamp deals with seconds - so we can add/subtract a certain number of seconds to modify the time by minutes, hours, or days.

$time = time();  //  Current time
$time = time() - 60;  //  One minute ago
$time = time() + 120;  //  Two minutes in the future
$time = time() - 3600;  //  One hour ago
PHP’s date() Function - Formatted Dates

The time() function and timestamps become really powerful when you understand how to use the date() function.

This takes two parameters - 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 php.net. Here are a few examples, though…

$now = time();
 
$x = date("m-d-Y", $now);  //  02-07-2008
$y = date("M d Y", $now);  // Feb 07 2008
$z = date("F d, Y", $now);  // February 07, 2008

Notice that the letters (mMFdY) represent the actual dates, while the spacing and punctuation is just used for formatting.

Getting Custom Timestamps with strtotime()

The last function to look at is strtotime(). This takes a formatted date string (like “Feb 07 2008″ or “1 day ago”) and returns the appropriate Unix timestamp.

This allows you to fetch a timestamp for pretty much any date you want and then manipulate it in other ways - using the date() function or your own mathematical functions.

This is also going to help us find a random date in between two given dates.

Finding a Random Date

In order to find a random date, we need three things. The rand() function - to get a random number or timestamp. A start date. An end date.

In this simple example, we’ll find a random time between the start of the Unix epoch (timestamp = 0) and the current time. Then we’ll format it in the m-d-Y format for easy readability.

$time = rand( 0, time() );
echo date("m-d-Y", $time);

Run that, and you’ll find a random date between “Jan 1 1970″ and whatever your current date is - “Feb 7 2008″ for me.

We can make this a little better by using strtotime to set the min and max value for rand(). In this example, we’ll find a date in 2007 - between “Jan 01 2007″ and “Dec 31 2007″.

$time = rand( strtotime("Jan 01 2007"), strtotime("Dec 31 2007") );
echo date("m-d-Y", $time);

And there you go. Replace the strtotime() calls that I used, and you can find a random date within any given daterange.

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 date() is around 1901. The latest date you’ll be able to work with is around 2025. This is a limitation based on the size of integers in PHP - the Unix timestamp just can’t get any bigger in its basic form.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , ,

One Comment to “How to Create a Random Date in PHP”

  1. Comment créer une date aléatoire en PHP said this on

    […]  How to Create a Random Date in PHP (0 visite) […]

Leave a Reply

buy cheap viagra buy cheap viagra online buy discount viagra online buy viagra from india buy online viagra buy viagra india buy viagra online cheap viagra cheap viagra online discount viagra discount viagra online viagra india rx get viagra online non prescription viagra order viagra order viagra india order viagra online viagra for sale viagra no prescription viagra on line buy viagra online no prescription best price for viagra buy cheapest viagra buy generic viagra buy generic viagra from india online buy sildenafil citrate buy sildenafil citrate online buy viagra cheap buy viagra cheap prices buy viagra online without prescription cheapest prices on generic viagra cheapest viagra cheapest viagra prices cheap generic viagra cheap sildenafil citrate discount viagra canada email viagra get viagra no prescription get viagra without prescription low price viagra mail order viagra mastercard viagra order generic viagra order sildenafil citrate viagra 50/100mg viagra online without prescription viagra order online viagra sildenafil citrate viagra soft tabs viagra soft tabs for sale viagra soft tabs online viagra soft tabs without prescription buy viagra soft tabs cheap viagra soft tabs discount viagra soft tabs generic viagra soft tabs order viagra soft tabs buy cheap kamagra buy cheap kamagra online buy discount kamagra online buy online kamagra buy kamagra india buy kamagra online cheap kamagra cheap kamagra online discount kamagra discount kamagra online get kamagra online non prescription kamagra order kamagra order kamagra india order kamagra online kamagra for sale kamagra no prescription kamagra on line buy kamagra online no prescription best price for kamagra buy cheapest kamagra buy generic kamagra buy kamagra cheap buy kamagra cheap prices buy kamagra online without prescription cheapest prices on generic kamagra cheapest kamagra cheapest kamagra prices cheap generic kamagra discount kamagra canada email kamagra get kamagra no prescription get kamagra without prescription low price kamagra mail order kamagra mastercard kamagra order generic kamagra kamagra online without prescription kamagra order online