Developers Archive for the 'php programming' Category

Counting working days excluding weekend

Counting working days excluding weekend Thursday, February 22nd, 2007

The following example illustrates how to calculate working days excluding week end days.

<?

function count_woking_days($date_start,$date_end){

$workdays = 5;

$firstdate = strtotime($date_start);

$lastdate = strtotime($date_end);

//get the day of the date

$firstday = date(w,$firstdate);

$lastday = date(w,$lastdate);

//get the week of the date

$firstweek = date(W,$firstdate);

$lastweek = date(W,$lastdate);

if($lastday > $workdays){

$lastday = $workdays;

}

if($lastday >= $firstday){

$leftday = $lastday - $firstday;

}else{

$leftday = $firstday - $lastday;

}

$diffweeks = $lastweek - $firstweek;

$dayinweeks = $diffweeks * $workdays;

return $totaldays = $dayinweeks + $leftday + 1;

}

$from_date = “2007-02-14″;

$to_date = “2007-02-28″;

echo count_woking_days($from_date,$to_date);

?>

In above example we can pass starting and ending date to “count_working_days()” function. In that funtion, we can calculate the totaldays except week end days.

How to check whether table exists in database

How to check whether table exists in database Tuesday, February 20th, 2007

<?

function check_table($host, $user, $pass, $db, $tbl)

{

$tables = array();

$link = @mysql_connect($host, $user, $pass);

@mysql_select_db($db);

$q = @mysql_query(”SHOW TABLES”);

while ($r = @mysql_fetch_array($q)) { $tables[] = $r[0]; }

@mysql_free_result($q);

@mysql_close($link);

if (in_array($tbl, $tables)) { return TRUE; }

else { return FALSE; }

}

if (check_table(’localhost’, ‘root’, ‘’, ‘db1′, ‘table1′)) {

echo ‘Table exists in database’;

} else {

echo ‘Table doesn’t exists’;

}

?>

The above specified example is used to check whether the table “table1″ is present in database “db1″. In check_table() function, we are passing host name, user name, password, database name and password. If table exists, check_table() function returns true, otherwise false.

Shortening text to specific characters

Shortening text to specific characters Friday, February 9th, 2007

In web pages, in some places we required to display the text according to page size. To do this, we can use a function to shorten / truncate a string of text into a specific number of characters and add three dots (…) to the end. This will also round the text to the nearest whole word instead of cutting off part way through a word.

Example

<?php

/**
* Add this to your page:
* <?php
* include “shorten_a_text_string.php”;
* echo ShortenText($text);
* ?>
* where $text is the text you want to shorten.
*
* Example
* Test it using this in a PHP page:
* <?php
* include “shortentext.php”;
* $text = “This is the text which will display 20 characters.”;
* echo ShortenText($text);
* ?>
*/

function ShortenText($text) {

//Here we can specify the number of characters to display
$chars = 20;

$text = $text.” “;
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,’ ‘));
$text = $text.”…”;

return $text;

}

?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.