Dec 03 2010

PHP find previous monday from a date

Category: UncategorizedGiulio Pons @ 11:29 am

This small function returns the date (with the format you want) of the previous monday from a given date. If given date is empty it gets the today date:

// usage:
// last monday: $d = findMonday();
// last monday before 31-12-2010: $d = findMonday("2010-12-31");
// last monday before 31-12-2010 with a specific format: $d = findMonday("2010-12-31","m/d/Y");

function findMonday($d="",$format="Y-m-d") {
	if($d=="") $d=date("Y-m-d");
	$delta = date("w",strtotime($d)) - 1;
	if ($delta <0) $delta = 6;
	return date($format, mktime(0,0,0,date('m'), date('d')-$delta, date('Y') ));
}
Share


Nov 25 2010

Get URL parameter in javascript

Category: JavascriptGiulio Pons @ 10:55 am

Sometimes in javascript you have the variable that you need to use in the url, as a parameter passed in GET to the page, but you don’t have it in page. You can retrieve those data analyzing the window.location.href string. Here is the Javascript function that does the work, pass to it the variable name of the variable you need:

function gup( varname ) {
	varname = varname.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+varname+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) return ""; else return results[1];
}

Another usefull function is the one that get the file name of the current page from the url.

Share

Tags: , , , , , , , ,


Nov 25 2010

Get file name in javascript

Category: JavascriptGiulio Pons @ 10:49 am

How to get the file name of the page? When you need to read the file name of the current page in javascript you can use the document.location.href string and parse it to find the filename, here is the script that strips away the other chars and keep only the script file name:

function getFileName() {
	var url = document.location.href;
	url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
	url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
	url = url.substring(url.lastIndexOf("/") + 1, url.length);
	return url;
}

Another usefull function is the one that get the parameters from the url.

Share

Tags: , , , , ,


« Previous PageNext Page »