Archive for March, 2007

Find Difference Between 2 Dates in PHP

This function will find the difference between two given times. Does PHP already have a native function for that?


//The difference between 2 given time
function dateDifference($day_1,$day_2) {
	$diff = strtotime($day_1) - strtotime($day_2);

	$sec   = $diff % 60;
	$diff  = intval($diff / 60);
	$min   = $diff % 60;
	$diff  = intval($diff / 60);
	$hours = $diff % 24;
	$days  = intval($diff / 24);

	return array($sec,$min,$hours,$days);
}

If you just want the day difference…


$diff = strtotime($day_1) - strtotime($day_2) + 1; //Find the number of seconds
$day_difference = ceil($diff / (60*60*24)) ;  //Find how many days that is

This can be done using the DATEDIFF function in MySQL 5

[tags]php,code,date,difference,time[/tags]

Backup Locally using Rsync

Backup a folder to another local folder using the rsync tool.


rsync -av ~/Scripts/ /var/Backup/Rsync/Scripts/

a = Archive Mode
v = Verbose

[tags]backup,linux,command,rsync,folder,local,tools[/tags]

Find the Fourth Sunday

Javascript code for finding the fourth sunday in any given month.


function findFourthSunday(year,month) {
	var fourth_sunday = 0;
	var sunday_count = 0;
	//There is a really cool solution for this
	//forth_sunday = 29 - first_day_of_month
	//But the problem is if the first of month is a sunday(first_day_of_month == 0), then there will be a bug. Found the bug in April 2007 ;-)
	for(var i=1;i<31;i++) {
		var a_day = new Date(year,month,i);
		if(a_day.getDay() == 0) sunday_count++;//'0' means sunday - 1 for monday, etc.

		if(sunday_count == 4) {
			fourth_sunday = i;
			break;
		}
	}
	return fourth_sunday;
}
function init() {
	var month_names = new Array("January","February","March","April","May","June","July","Augest","September","October","November","December");
	var first_day = ""
	var today = new Date();
	var year = today.getYear();
	year = (year > 2000) ? year : year + 1900;
	var month = today.getMonth();

	var fourth_sunday = findFourthSunday(year,month);
	if(today.getDate() > fourth_sunday) {
		month ++;
		fourth_sunday = findFourthSunday(year,month);
	} else if(today.getDate() == fourth_sunday) {
		fourth_sunday = "Today - " + fourth_sunday;
	}
	var date = fourth_sunday + " " + month_names[month] + ", " + year;
	alert(date);
}
window.onload=init;

[tags]javascript,fourth,sunday,calender,month,date,day,code[/tags]

Packaging a Subversion Project

This is the method that I use to package a SVN Project. For CVS projects, the code is slightly different. Please note that the second line requires bash to work.

This is how I packaged nexty


svn checkout https://nexty.svn.sourceforge.net/svnroot/nexty
find nexty/ -name .svn -exec rm -rf {} \;
tar -czf nexty.tar.gz nexty/
rm -rf nexty/

[tags]svn,package,zip,compress,project,subversion[/tags]

Using Subversion - Import, Checkout and Update

Import/New Project

svn import <Folder> <Repository>

Online Repository Example

svn import Nexty https://nexty.svn.sourceforge.net/svnroot/nexty

Checkout

svn checkout https://nexty.svn.sourceforge.net/svnroot/nexty [<folder>]

Update

svn update

[tags]svn,subversion,version control,command,cli,import,checkout,update[/tags]

Recording Sound in Linux

To record a sound in wav format from Mic, just use this command…

arecord -f cd test.wav

Use Ctrl+C to stop recording

To playback the recording, use this command…

aplay test.wav

Text to Speech - Hear a File

Festival is a speech synthesizer for Linux. You can use this with the following commands…

Enter a festival console for more operations

festival
festival> (SayText "Hello world! Isn't the weather excellent today.")

Read a file…

festival --tts hello.txt

Just read some text.

echo "Hello world! Isn't the weather excellent today." | festival --tts

Original Article

[tags]linux,command,speech,text,read,voice[/tags]

MySQL Database Backup/Restore

This command can be used to backup MySQL database.

mysqldump -u <User> -p <Database name> [<Table name>] > backup.sql

This command will create a file ‘backup.sql’ that will have all the data of the Database. The <Table name> is optional.

The database can be restored from such a dump using the command

mysql -u <User> <Database name> < backup.sql

Example

I backup all the task in Nexty using this command.

mysqldump -u root Apps_Nexty > nexty.sql

This will create a file called ‘nexty.sql’ - which can be restored using the command…

mysql -u root Apps_nexty < nexty.sql

[tags]mysql,backup,import,export,restore,database,command,dump,sql[/tags]

Shortest way to create an XMLHttpRequest object

For best results use a framework - like jx

http = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null);

Original Article
[tags]javascript,ajax,code,xhr[/tags]

Command to Replace a String in all Files in Current Directory

Replaces all instance of ‘<old string>’ with ‘<new sting>’ in all the files of the current directory.

perl -pi -e "s/<old string>/<new string>/g;" *

[tags]perl,command,rename,replace,string[/tags]