Categories
How to

How to move Winamp playlist entries fast to the begining or the end of the list when having a long playlist

Selecting the Winamp entries:
1. Click on one entry in the list that you want to move.
2. If you want to move several entries, you can select them in one of the following ways:
– to select a block of entries starting with the entry chosen at step 1 (onwards/backwards from that entry):
SHIFT + left mouse button click on the last entry to be moved
or
SHIFT + up/down arrow
– to select non consecutive entries including the entry chosen at step 1: CTRL + left mouse button click on every other entry to be moved.

Moving the selected entries to the begining/end of the Winamp playlist:
1. Left mouse button click and hold on one of the selected entries and press HOME/END key on the keyboard so that you end up on the first/last page of the playlist. Don’t release the mouse button!
2. Drag the selection with the mouse to the very begining/end of the playlist.

Enjoy your audition!

Categories
How to PHP

How to convert server date and time to the local date and time taking into account daylight saving time

‘Many countries, or even parts of countries, adopt daylight saving time (DST, also known as “Summer Time”) during part of the year. This typically involves advancing clocks by an hour near the start of spring and adjusting back in autumn (“spring” forward, “fall” back).’ (see Wikipedia)

Now I will demonstrate how we can convert a server date and time to the corresponding date and time in another timezone in Europe, taking Romania as example.

‘All countries in Europe except Iceland observe DST, and most change on the same date and time, starting on the last Sunday in March and ending on the last Sunday in October.’
‘In the West European (UTC), Central European (CET, UTC+1), and East European (UTC+2) time zones the change is simultaneous: on both dates the clocks are changed everywhere at 01:00 UTC, i.e. from local times of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in October.’ (see Wikipedia)

Romania is a country in the Eastern Europe, which falls under the East European Time (EET) also known as UTC+2 time zone and uses Eastern European Summer Time (UTC+3) as a summer daylight saving time.

The next function will be used to find out the offset in hours from UTC (GMT) of a local date and time of Romania or another region that switches between DST and standard time on the same date and time as Romania (is the local date and time in DST or in standard time?)

function local_offset_from_utc($timestamp, $standard_offset)
//$timestamp - the timestamp of the server date and time we want to convert to local date and time
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1
{
	$year = gmdate("Y", $timestamp); //get the UTC year of the given date and time
//get the date to 'spring forward'
	$mar_31_utc_ts = gmmktime(0, 0, 0, 4, 0, $year); //the UTC timestamp of March 31, the last day of March, equivalent to gmmktime(0, 0, 0, 3, 31, $year)
	$last_sun_mar = 31 - gmdate("w", $mar_31_utc_ts); //the day of the month under which falls the last Sunday in March
	$spring_ts = gmmktime(1, 0, 0, 3, $last_sun_mar, $year); //the timestamp of the last Sunday in March 01:00 UTC
//get the date to 'fall back'
	$oct_31_utc_ts = gmmktime(0, 0, 0, 11, 0, $year); //the UTC timestamp of October 31, the last day of October, equivalent to gmmktime(0, 0, 0, 10, 31, $year)
	$last_sun_oct = 31 - gmdate("w", $oct_31_utc_ts); //the day of the month under which falls the last Sunday in October
	$fall_ts = gmmktime(1, 0, 0, 10, $last_sun_oct, $year); //the timestamp of the last Sunday in October, 01:00 UTC
	if($timestamp >= $spring_ts && $timestamp < $fall_ts) //daylight saving time
	{
		return $standard_offset + 1;
	}
//standard time
	return $standard_offset;
}

If the server switches between DST and standard time simultaneously with your timezone, you can simply use date("I") to find out if your local date and time is in daylight saving time or standard time:

if(date("I", $timestamp) == 1)
{
	$offset = $standard_offset + 1;
}
else
{
	$offset = $standard_offset;
}

Be careful with that, because a sever don't always switch between DST and standard time at the same hour as the timezone it has set, for example my server switches from DST to standard time at 03:00 UTC+3, which becomes 02:00 UTC+2, when it should go from 04:00 UTC+3 to 03:00 UTC+2). So I better use the function above that works not caring if or when the timezone of the server switches between DST and standard time.

If an offset of a timezone from UTC is negative, then the UTC date and time is greater than the corresponding date and time in the considered timezone; if the offset is positive then the UTC date and time is lower than the corresponding date and time in the considered timezone. If we consider t being the date and time in a given timezone, utc being the corresponding UTC date and time and offset being the offset of the given timezone from UTC, then we have:

t = utc + offset
utc = t - offset

So if we consider t1 being a server date and time and o1 being the offset of the timezone of the server from UTC (in hours) and t2 being the local date and time and o2 being the offset of the timezone of the server from UTC (in hours) we have:

utc = t1 - o1
t2 = utc + o2 = t1 - o1 + o2

In practice we only have a server date and time which we convert to timestamp ts1 based on which we find out the timestamp ts2 when the date and time in the timezone of the server equaled/will equal to the local date and time corresponding to ts1:

ts2 = ts1 - os1 + os2, where os1 = o1 * 60 *60 seconds and os2 = o2 * 60 *60 seconds

E.g. Let's consider the timezone of the server date and time is UTC-5 and Romania is in standard time (has an offset of 2 hours from UTC).

The UTC date and time that corresponds to the given timestamp equals to the server date and time that was/will be 5 hours later than the given timestamp:

ts_aux = ts1 - (-5 * 3600) = ts1 + 5 * 3600

The local date and time of Romania corresponding to the given timestamp equals to the UTC date and time that was/will be 2 hours later than the UTC date and time computed above:

ts2 = ts_aux + 2 * 3600 = ts1 + (5 + 2) * 3600 = ts1 + 7 * 3600

But this is NOT true if the server time and date that equals the needed local date and time falls under DST and the original server date falls under standard time or vice versa, which means that the offsets from UTC of the two server dates are different by 1 hour.

For example, if the server we considered before is in UTC-5 DST and goes back one hour on the first of November at 03:00:00 local server time and we want to see what Romania local date and time corresponds to the server date and time November 1st 01:00:00, then, applying the formula above, we will obtain a timstamp that corresponds to the server date and time November 1st 07:00:00 which should correspond to the local Romania time for the original server time, but the correct Romania date and time being November 1st 08:00:00. That is because, between November 1st 01:00:00 and November 1st 07:00:00, the server goes back one hour so that 03:00:00 DST becomes 02:00:00 standard time and 08:00:00 becomes 07:00:00, while the Romania time does not, Romania switching to DST one week earlier, so the correct formula in this case is:

ts2 = ts1 - os1 + os2 + 3600
ts2 = ts1 + (5 + 2 + 1) * 3600 = ts1 + 8 * 3600

Similarly, if the server switches from standard time to DST between the two dates, the formula becomes:

ts2 = ts1 - os1 + os2 - 3600
ts2 = ts1 + (5 + 2 - 1) * 3600 = ts1 + 6 * 3600

The next function is used to determine what the timestamp was/will be in the timezone of the server when the server date and time equaled/will be equal to the needed local date and time.

function get_timestamp_for_local($date = "", $standard_offset = 2)
{
	if(!empty($date))
	{
		$timestamp = strtotime($date);
	}
	else
	{
		$timestamp = time();
	}
	$os1 = date("Z", $timestamp);
	$os2 = local_offset_from_utc($timestamp, $standard_offset) * 3600;
	$ts = $timestamp - $os1 + $os2;
	//check the offset from UTC of the server date and time that equal the needed local date and time
	$os3 = date("Z", $ts);
	if($os3 == $os1) //the two server dates are both in DST or both in standard time
	{
		return $ts;
	}
	elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time
	{
		return $ts - 3600;
	}
 	//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST
	return $ts + 3600;
}

And finally we get the needed date and time:

function get_ro_date($date)
{
	return date("d.m.Y H:i:s", get_timestamp_for_local($date));
}

Next l will show how we can convert a server date and time to the corresponding date and time in another timezone in North America, taking the State of Washington as example.

'North America generally follows the same procedure, with each time zone switching at 02:00 LST (local standard time) to 03:00 LDT (local daylight time) on the second Sunday in March, and back from 02:00 LDT to 01:00 LST on the first Sunday in November since 2007.'
In the United States of America, 'the start of DST now occurs on the second Sunday in March and ends on the first Sunday in November.' (see Wikipedia)

The State of Washington falls under the Pacific Time Zone (PT), which is Pacific Standard Time (PST) or UTC-8 when observing standard time and Pacific Daylight Time (PDT) or UTC-7 during daylight saving time.

So we have to change the previous local_offset_from_utc function to find out the offset in hours from UTC (GMT) of a local date and time of Washington:

function local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour)
//$timestamp - the timestamp of the server date and time we want to convert to local date and time
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1
//$spring_forward_hour - the hour of the local time when switching to DST
//$fall_back_hour - the hour of the local time when switching to standard time
{
//find out the year of the local date and time corresponding to the given server date and time, based on the corresponding UTC date and time
	$year = gmdate("Y", $timestamp); //UTC year of the given server date and time
	if($standard_offset < 0) //local timezone is behind UTC
	{
		//if the given sever date and time corresponds to January the 1st in UTC and the UTC hour is lower than the absolute value of the offset of the local time from UTC, then the corresponding local date and time falls in the year previous to the UTC year
		$month = gmdate("n", $timestamp);
		if($month == 1)
		{
			$day = gmdate("j", $timestamp);
			if($day == 1)
			{
				$hour = gmdate("G", $timestamp);
				if($hour + $standard_offset < 0)
				{
					$year -= 1;
				}
			}
		}
	}
	else //local timezone time is ahead of UTC
	{
		//if the given sever date and time corresponds to December 31 in UTC and the sum of UTC hour and the offset of the local time from UTC is greater than 24, then the corresponding local date and time falls in the year subsequent to the UTC year
		$month = gmdate("n", $timestamp);
		if($month == 12)
		{
			$day = gmdate("j", $timestamp);
			if($day == 31)
			{
				$hour = gmdate("G", $timestamp);
				if($hour + $standard_offset > 24)
				{
					$year += 1;
				}
			}
		}
	}
	//get the date to 'spring forward'
	$mar_1_utc_ts = gmmktime(0, 0, 0, 3, 1, $year); //the timestamp for the UTC time of March the 1st, 00:00:00
	$mar_1_day_of_week = gmdate("w", $mar_1_utc_ts);
	//compute the day of the month under which falls the second Sunday in March
	if($mar_1_day_of_week > 0) //the day of the week of the 1st of March is not Sunday
	{
		$second_sun_mar = 15 - $mar_1_day_of_week;
	}
	else
	{
		$second_sun_mar = 8;
	}
	$spring_local_ts = gmmktime($spring_forward_hour - $standard_offset, 0, 0, 3, $second_sun_mar, $year);
	//get the date to 'fall back'
	$nov_1_utc_ts = gmmktime(0, 0, 0, 11, 1, $year); //the timestamp for the UTC time of November the 1st, 00:00:00
	$nov_1_day_of_week = gmdate("w", $nov_1_utc_ts);
//compute the day of the month under which falls the first Sunday in November
	if($nov_1_day_of_week > 0) //the day of the week of the 1st of November is not Sunday
	{
		$first_sun_nov = 8 - $nov_1_day_of_week;
	}
	else
	{
		$first_sun_nov = 1;
	}
	$fall_local_ts = gmmktime($fall_back_hour - ($standard_offset + 1), 0, 0, 11, $first_sun_nov, $year);
	if($timestamp >= $spring_local_ts && $timestamp < $fall_local_ts) //daylight saving time
	{
		return $standard_offset + 1;
	}
	//standard time
	return $standard_offset;
}

The function get_timestamp_for_local remains the same, only we add 2 more paramters to it which will be needed when calling the function:

function get_timestamp_for_local2($date = "", $standard_offset = -8, $spring_forward_hour = 2, $fall_back_hour = 3)
{
	if(!empty($date))
	{
		$timestamp = strtotime($date);
	}
	else
	{
		$timestamp = time();
	}
	$os1 = date("Z", $timestamp);
	$os2 = local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour) * 3600;
	$ts = $timestamp - $os1 + $os2;
	//check the offset from UTC of the server date and time that equal the needed local date and time
	$os3 = date("Z", $ts);
	if($os3 == $os1) //the two dates are both in DST or both in standard time
	{
		return $ts;
	}
	elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time
	{
		return $ts - 3600;
	}
	//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST
	return $ts + 3600;
}

And now we can find out the date and time in the State of Washington that corresponds to the given server date and time:

function get_wa_date($date)
{
	return date("Y-m-d H:i:s", get_timestamp_for_local2($date));
}
Categories
How to

How to recover a hard deleted store folder in Microsoft Outlook Express 6

It happened for several times that I accidentally hard deleted a store folder in Microsoft Outlook Express 6 when trying to delete an email in that folder (both the folder and the e-mail looked like being selected, but it seems that the focus was on the folder and not the e-mail as I expected).

Seems that when we access (select) for the first time a store folder, say “MyFolder”, we created in Outlook Express, a .dbx file is automatically created in the Outlook Express Mail directory “C:\Documents and Settings\<username>\Local Settings\Application Data\Identities\{<identity-code>}\Microsoft\Outlook Express”.
If it doesn’t already exist a file named MyFolder.dbx in the OE Mail directory, such a file is created and a refernce from the store folder “MyFolder” to this .dbx file is created in the Folder.dbx file in the OE Mail directory.
If there already is a .dbx file in The OE Mail directory with the same name as the folder we created (MyFolder.dbx), then, when we access the folder “MyFolder”, it will be created a .dbx file named MyFolder(1).dbx, if does not already exist a file named MyFolder(1).dbx in the OE Mail directory, otherwise a file named MyFolder(2).dbx will be created etc. Also a reference from “MyFolder” to the newly created .dbx file is created in the Folders.dbx file.

To see which .dbx file is associated with a certain store folder in Outlook Express, right click on that folder and select Properties; under the General tab you’ll find the full path to the .dbx file the folder is stored in.

When we delete a store folder in Outlook Express, only the reference to the .dbx file is deleted, the .dbx file remaining orphan. So, if we want to recover the deleted folder, a new folder and a reference between it and the orphaned .dbx file should be created. This is how you do that:

1. Move, not copy, the orphaned .dbx file from OE Mail directory to another location.
Do not just copy the file elsewhere because if there already is a .dbx file with the same name as the folder you create, say MyFolder.dbx, then when you select the newly created folder it will be created a .dbx file named like MyFolder(1).dbx as explained before.

2. In Outlook Express, create a folder with exactlty the same name (case sensitive) as the orphaned .dbx file, select the newly created folder so that a new .dbx file is automatically created (this should have the same name as the orphaned .dbx file), close OE.

3. Overwrite the new (empty) .dbx file in your OE Mail directory with the orphaned file and open OE.

4. If you had any mail message rules involving the deleted folder, repair them, because OE deletes the references to the folder from the message rules once you delete a folder, but it does not remake them once you remake the folder (go to Tools -> Messages rules -> Mail).

Categories
Flex How to

How to dynamically add attributes and child nodes to an XML element in Flex

This is how I copied all the attributes of an XML node (srcXMLNode) to another XML node (dstXMLNode) in Flex, without knowing the names of those attributes:

for each(var attr:XML in srcXMLNode.attributes())
{
dstXMLNode.@[attr.name()] = attr;
}

And this is the way I added child nodes of an XML element to another:

for each(var elem:XML in srcXMLNode.elements())
{
dstXMLNode[elem.name()] = elem;
}

If you know a better way to do this, feel free to write a comment.

Categories
How to

How to uninstall Google Toolbar for Firefox

Problem: Because I did not use Google Toolbar (it was automatically installed when I installed Firefox), I decided to uninstall it. I selected the Uninstall option in Tools -> Add-ons -> Google Toolbar in Firefox (Disable option being grayed) and it said that the toolbar will be uninstalled the next time Firefox will restart. So I closed and started Firefox again and I got a popop window that said something like thanks for installing Google Toolbar and if I want to see the page rank for every site I visit etc. This window kept appearing until I agreed installing Google Toolbar. I also tried uninstalling the toolbar using its uninstall setting, but I kept getting the same (reversed) result.
Everytime I closed the Firefox browser window I got sure that the procees was closed (no instances of FF remained opened).
My operating system is Windows XP Professional.

The solution that worked for me consists of two simple steps:
1. I uninstalled Google Toolbar for Internet Explorer using Control Panel -> Add/Remove programs.
2. I pressed Disable (not grayed anymore) and then Uninstall buttons for Google Toolbar in Firefox Tools -> Add-ons and restarted Firefox.

And it worked! No annoying popop window anymore.

Categories
How to MySQL PHP Tutorial

How to use specific language characters with PHP and MySQL (example: Romanian)

Problem: Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones.

My fix for this problem is using UTF-8 character set encoding for every page of the website and the MySQL tables that contain the fields you are using. Also all the html encodings from PHP use the UTF-8 character set encoding (this is not mandatory).

If you already have the database, but with the default character set (latin1) and collation (latin1_swedish_ci) for the tables with text fields (of type CHAR, VARCHAR, TEXT etc) in which you need to have special characters, you should change the character set of each of those tables like this:

ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;

If you don’t have the database then you should create it and when you create a table that you need to use with specific language characters, you should specify the character set for that table:

CREATE TABLE `my_table` (
`idmy_table` tinyint(3) unsigned NOT NULL auto_increment,
`my_field` varchar(255) NOT NULL default '',
PRIMARY KEY  (`idmy_table`)
) CHARSET=utf8;

The most important thing is that in PHP, after opening a database connection, before executing any query to the database, you should ensure that this code is executed

mysql_query("SET NAMES utf8", $my_conn);

This tells the server what character set the client is using for sending SQL statements and the character set the server should use to return the results to the client.

A simple example:

<?php
$my_conn = @mysql_connect("localhost", "user", "pass")
or die("There was a problem connecting to MySQL. Please try again later.");
if(!@mysql_select_db("test", $my_conn))
{
die ("There was a problem connecting to the database. Please try again later.");
}
mysql_query("SET NAMES utf8", $my_conn);
if(!empty($_GET['mystr']))
{
// insert the string into the database
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";
$result = mysql_query($query, $my_conn);
if($result)
{
// save the id of the table row inserted
$last_insert_id = mysql_insert_id($my_conn);
// get the last inserted value
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";
$result = mysql_query($query, $my_conn);
if($result && $row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$db_string = $row['my_field'] ;
}
}
}
elseif(!empty($_GET['searchstr']))
{
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";
$result = mysql_query($query, $my_conn);
if($result)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$search_results[$row['idmy_table']] = $row['my_field'];
}
}
}
mysql_close($my_conn);
?>
<html>
<head>
<title>Page title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<?php
if(!empty($db_string))
{
echo "<strong>Inserted string</strong>: $db_string<br />";
}
?>
<form method="get" action="">
String to insert into the database <input type="text" name="mystr"/>
<input type="submit" value="GO"/>
</form>
<?php
if(!empty($search_results))
{
echo "<strong>Search results</strong>:<br />";
foreach($search_results as $id => $value)
{
echo $value."<br />";
}
}
?>
<form method="get" action="">
Search query <input type="text" name="searchstr"/>
<input type="submit" value="GO"/>
</form>
</body>
</html>

Tip: The search in Romanian language over the database (tested with MySQL LIKE operator) works like a charm when searching words that have special characters or not.

For example: In Romanian language the word “peasant” is written as “ţăran” and someone who searches it gets the same result for the search terms “taran” or “ţăran” or “ţaran” or “tărân” and so on – so this is the real magic.

UPDATE: You may also need to add a header to the php script if you use ob_start or similar php functions like this:

header("Content-type: text/html; charset=UTF-8");

this usually fixes the encoding selection in Internet Explorer for this case.

Categories
Apache Web Server How to PHP Tutorial

How to receive a failure notice when the recipient cannot be reached after sending an email using the PHP mail() function

I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code:

$subject = "Email subject";
$message = "line1\r\nline2\r\nline3";
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "To: <$to_email>\r\n";
$headers .= "From: Me <$my_email_address>\r\n";
$headers .= "Return-Path: <$my_return_email_address>r\n";
mail($to_email, $subject, $message, $headers);

The problem was that the email address I wanted to receive the failure notices to ($my_return_email_address) was not the same as the value of the configuration option sendmail_from in the php.ini file (Apache web server installed on a machine with the Windows Professional operating system). So the failure notices were sent to the sendmail_from email address if this was an existing address, instead of the email address specified in the Return-path header of the email.

The solution is replacing the lines:

$headers .= "Return-Path: <$my_return_email_address>r\n";
mail($to_email, $subject, $message, $headers);

with:

// when the PHP server runs on Windows
ini_set(sendmail_from, $my_return_email_address);
mail($to_email, $subject, $message, $headers);
ini_restore(sendmail_from);
// when the PHP server runs on UNIX
mail($to_email, addslashes($subject), $message, $headers, "-r $my_return_email_address");

This means that, before sending the email, we set the value of the sendmail_from configuration option to $my_return_email_address and we restore the default value of the configuration option after the email is sent.

Categories
How to HTML JavaScript Tutorial

How to check/uncheck a bunch of checkboxes without using ids for the checkbox inputs

My solution of checking/unchecking a group of HTML checkboxes using Javascript implies using an array of checkboxes, which means naming all the inputs of type ‘checkbox’ like array_name[]. Example:

<form name="cb_form">
<input type="checkbox" name="cb[]" value="0" />Zero
<input type="checkbox" name="cb[]" value="1" />One
<input type="checkbox" name="cb[]" value="2" />Two
<input type="checkbox" name="cb[]" value="3" />Three
</form>

In this example, the name of the checkboxes array is cb.

Next, we place two links for checking/unchecking all checkboxes in our array. If someone clicks one of these links, the checkAll() JavaScript function is called:

<a href="" onclick="checkAll('cb_form', 'cb[]', true); return false;">Check all<a>
<a href="" onclick="checkAll('cb_form', 'cb[]', false); return false;">Uncheck all<a>

The checking/unchecking all checkboxes function in JavaScipt looks like this:

function checkAll(form_name, cb_name, value)
{
var cb_arr = document.forms[form_name].elements[cb_name];
// if the checkboxes exist
if(cb_arr)
{
// if the number of checkboxes is at least 2
if(cb_arr.length > 1)
{
// for each checkbox
for(i = 0; i < cb_arr.length; i++)
{
// check (value == true) or uncheck (value == false) it
cb_arr[i].checked = value;
}
}
else // cb_arr.length is undefined which means there is a single checkbox element that is not considered an array of one element
{
cb_arr.checked = value;
}
}
}

Note that if we only have one checkbox, the variable cb it is not considered an array, but a normal variable. This is useful in the situation of dinamically generated HTML pages (using PHP, for example) and the number of checkboxes varies from page to page.

You can test the example here:

Zero One Two Three

Check all Uncheck all