Thursday, September 3, 2009

Generating numbers with Date and Time using ColdFusion and PHP

Recently I was asked to create a simple script either in ColdFusion or PHP that would generate a unique number, so I said why not have some fun using date and time and code it in both languages. Both scripts will generate a number containing of 23 numeric characters. Took me about 5 minutes to get everything coded and tried to keep the examples in both ColdFusion and PHP as similar as possible and I'm sure there's an easier way to do this. So please don't say Hey! you can do this and that and this and that lol.

Both scripts will generate a number similar to 42453609032009061855012

<cfset d = Now()>
<cfset day_week = DayOfWeek(d)>
<cfset day_year = DayOfYear(d)>
<cfset week_year = Week(d)>
<cfset month_year = DateFormat(d, "mmddyyyy")>
<cfset time_hour1 = TimeFormat(d, "hhHHmmss")>
<cfset ts = TimeFormat(d, "tt")>

<cfswitch expression="#ts#">
<cfcase value="am">
<cfset time_span = 1>
<cfcase value="pm">
<cfset time_span = 2>

<cfset date = "#day_week##day_year##week_year##month_year#">
<cfset time = "#time_hour1##time_span#">
<cfset date_time = "#date##time#">


$d = date("a");
switch ($d) {
case "am":
$day_time = 1;

case "pm":
$day_time = 2;

$date_time = "wzWmdYhHis";
$dt = date("$date_time$day_time");

echo $dt;


James said...

*cough* createUUID() *cough, cough* :)

Blue Web Techniques said...

Aha!!! what about the possibility of dupes (although a rarity). It has and can happen and also createUUID() seems to be known to put a drag on the server. :)