drew

Words on your display.

Archive for the ‘PHP’ tag

PHP Time Based UUID Function (GUID)

with one comment

In my desire to find the “right” UUID function to use in my application, I grew to like the MySQL UUID() results produced because they are partially time based. But I didn’t like that I have to make a database call every time I want a new UUID.  I didn’t find any similar functionality in PHP or from user based PHP functions, so here’s my efforts.

It clocks in at 100,000 results in about 1.36 seconds on my 2.8Ghz Intel iMac.

function uuid()
{
	// Time based PHP Unique ID
	$uid = uniqid(NULL, TRUE);
	// Random SHA1 hash
	$rawid = strtoupper(sha1(uniqid(rand(), true)));
	// Produce the results
	$result = substr($uid, 6, 8);
	$result .= substr($uid, 0, 4);
	$result .= substr(sha1(substr($uid, 3, 3)), 0, 4);
	$result .= substr(sha1(substr(time(), 3, 4)), 0, 4);
	$result .= strtolower(substr($rawid, 10, 12));
	// Return the result
	return $result;
}

We produce results looking like this:

1f8fec61-4a6f-9bd5-7f8e-e12325c464c3
1f8fed49-4a6f-9bd5-7f8e-d665dedc0297
1f8fee17-4a6f-9bd5-7f8e-29d1ce3e9e30
1f8feef2-4a6f-9bd5-7f8e-8ce03bebdbdf
1f8fefc7-4a6f-9bd5-7f8e-c22fbedb5390

Written by Drew

July 28th, 2009 at 3:50 pm

Posted in PHP, Programming, WebDev

Tagged with , , , ,

Converting Ticks to a Timestamp

with 2 comments

I recently had to deal with a SQLite database that stored the timestamps as ticks.  Having to first research what a tick is, I created a simple function that will convert a tick value to a timestamp or MySQL datetime value since I couldn’t find anyone else who had done such a thing yet.

You can download it here.

The only tricky part (aside from figuring out what a tick is) was calculating the number of ticks between 0001-01-01 to 1970-01-01. Also of note: a tick is apparently a popular way to store date/time values for Microsoft.

Written by Drew

May 2nd, 2009 at 3:12 pm

Posted in PHP, Programming, WebDev

Tagged with , , ,

Serving a large file through PHP without hitting memory_limit

without comments

Ran into a little problem hitting the memory limit for PHP when serving 100MB+ files through a script like:

$file = @fopen($filename,"r");

if ($file)
{
while(!feof($file))
{
    print(fread($file, 1024*4));
    flush();
    ob_flush();
}
@fclose($file);
}

Found that you need to call ob_flush() as well as flush() since flush() has no effect on the buffering scheme of your web server.

Just thought I’d throw that out there for anyone else.

Written by Drew

September 10th, 2008 at 11:44 pm