Tag Archives: php

Sum Businnes Day with PHP

function sum_business_day($date, $days) {
 
	list($year, $month, $day) = explode('-', $date);
 
	$business_day = 0;
	$add_day = 0;
 
	while ($add_day < $days) {
 
		$business_day++;
 
		$wday = gmdate('w', strtotime('+' . $business_day . ' day', mktime(0, 0, 0, $month, $day, $year)));
 
		if ($wday != 0 && $wday != 6) {
 
	            $add_day++;
		}
	}
 
	return gmdate('Y-m-d', strtotime('+ ' . $business_day . ' day', strtotime($date)));
}

Get xml node attribute with PHP

$xml = simplexml_load_file(path/to/your/xml); // or simplexml_load_string('xml_string')
 
(string) $xml->your_node->attributes()->your_attribute;

Easy generate html forms with php

https://github.com/zech86/formx

Validate .xml with .xsd in php

$x_validate = new DOMDocument();
$x_validate->loadXML(xml_string); or $x_validate->load(path/to/file.xml);
 
// return bool
$x_validate->schemaValidate(path/to/xsd.xsd);

Max days in a month with PHP

function get_max_days_in_month($month, $year) {
 
	$getdate = getdate(mktime(null, null, null, $month + 1, 0, $year));
 
	return $getdate['mday'];
}

Last weekday in a month with PHP

function get_last_weekday_in_month($month, $year) {
 
	$getdate = getdate(mktime(null, null, null, $month + 1, 0, $year));
 
	return $getdate['wday'];
}

First weekday in a month with PHP

function get_first_weekday_in_month($month, $year) {
 
	$getdate = getdate(mktime(null, null, null, $month, 1, $year));
 
	return $getdate['wday'];
}

chmod recursive

function chmod_r($path, $filemode) {
 
	if (!is_dir($path)) {
 
  		return chmod($path, $filemode);
 	}
 
 	$dh = opendir($path);
 
 	while ($file = readdir($dh)) {
 
	  if ($file != '.' && $file != '..') {
 
		$fullpath = $path.'/'.$file;
 
	   	if (!is_dir($fullpath)) {
 
	    		if (!chmod($fullpath, $filemode)) {
 
	     			return false;
	    		}
 
	   		} else {
 
	    		if (!chmod_r($fullpath, $filemode)) {
 
	     			return false;
	    		}
	   	}
	  }
	}
 
 	closedir($dh);
 
 	if (chmod($path, $filemode)) {
 
  		return true;
 	}
 
 	return false;
}

usage:

chmod_r('your/path', 0777);

PHP Rest with cURL

request.php

function request($request_type) {
 
	$ch = curl_init();
 
	curl_setopt ($ch, CURLOPT_URL, "http://localhost/server.php");
 
	switch (true) {
 
		case $request_type == 'get':
 
			curl_setopt ($ch, CURLOPT_HEADER, 0);
 
			break;
 
		case $request_type == 'post':
 
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, array('foo' => 'bar'));
 
			break;
 
		case $request_type == 'put' :
 
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
 
			break;
 
		case $request_type == 'delete':
 
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
 
			break;
 
	}
 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
	return curl_exec($ch);
}
 
var_dump(request('get'));
 
var_dump(request('post'));
 
var_dump(request('put'));
 
var_dump(request('delete'));

server.php

echo 'REQUESTED: ' . $_SERVER['REQUEST_METHOD'];

Smarty with php count function

{if $myarray|@count gt 0}...{/if}
Page 1 of 212