Short Tag – Web Development Blog

Entries for April, 2010

Datediff function in PHP (similar to VB)

A function that calculates the difference between 2 dates.  It is very similar to the datediff function available in Visual Basic.

function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {

if (!$using_timestamps) {

$datefrom = strtotime($datefrom, 0);

$dateto = strtotime($dateto, 0);


$difference = $dateto – $datefrom; // Difference in seconds

switch($interval) {

case ‘yyyy’: // Number of full years

$years_difference = floor($difference / 31536000);

if [...]

htmlspecialchars_decode for PHP4

Sometimes we do not have all the things we want on a webserver and on shared hosting we cannot do much about it, so we find workarounds. This is the equivalent of the htmlspecialchars_decode function in PHP4. htmlspecialchars_decode is available only from PHP5, so this little function of mine can be quite handy on PHP4.

if [...]

Unzip with PHP

To unzip with PHP you need to have your PHP server compiled with php_zip.dll on Linux or the php_zip.dll extension active on Windows(activate it from php.ini).

$zip = zip_open("");
//first we open the zip file, $zip is a file handler

if ($zip) {
while ($zip_entry = zip_read($zip)) {
//reading each zip entry (zip file)

$fp = fopen("zip/".zip_entry_name($zip_entry), "w");
//open the file for [...]

getElementsByClassName in Javascript

This is a function that returns all the elements in the page that have the given class name.
Returns an array with the objects.

function getElementsByClassName( strClassName, obj ) {
var ar = arguments[2] || new Array();
var re = new RegExp("\\b" + strClassName + "\\b", "g");

if ( re.test(obj.className) ) {
ar.push( obj );
for ( var i = 0; i [...]

Set cookie with javascript

Sometimes is useful to set a cookie with javascript. You can use the function below.

function Set_Cookie(name, value, expires, path, domain, secure )
var today = new Date();
today.setTime( today.getTime() );

if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + [...]

Stage.align in Flash

Stage.align property sets the 0,0 coordinates of the Stage to a certain position.
For example:
Stage.align = “TL”; – sets the 0,0 coordinates to the top-left part of the Stage.
Stage.align = “CC” or Stage.align=”” – the default position is in the center of the Stage.
Stage.align property values:










C – use C in combination where you need to represent [...]

Change the character set of your page with PHP and HTML

When using special characters you need to set the character set of your page, otherwise each browser uses the character set it pleases.
Let’s say my backend uses UTF-8 character set, then my frontend should use UTF-8 as well.
HTML code:
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
PHP code:
header(‘Content-type: text/html; charset=”utf-8″‘);
I use the both , but one should be [...]

Get the domain of an e-mail address with PHP

function getEmailDomain($email)
$email = trim($email);
$emails = explode(@, $email);
return trim($emails[1]);