Archive for the ‘PHP Tutorial’ Category

How to Detemine The Number Of Fridays In A Month

The following code determines the number of Fridays in a month :
$year = 2008;
$month = 1; //Aug
$day = 1;
$startDate = mktime(0, 0, 0, $month, $day, $year);
$NoD = date( “t”, $startDate);
While(date( “D”, $startDate) != “Fri”) //find first Friday
{
$day++;
$startDate = mktime(0, 0, 0, $month, $day, $year);
}
$day–;
$days = ceil(($NoD-$day)/7);
echo “$days Fridays”;
Replace the $month=1; to the number of the [...]

Function to Get the variable name

This is a PHP Function to get the name of the variable (not the value) :
function getvarname(&$var)
{
$ret = ”;
$tmp = $var;
$var = md5(uniqid(rand(), TRUE));
$key = array_keys($GLOBALS);
foreach ( $key as $k )
if ( $GLOBALS[$k] === $var )
{
$ret = $k;
break;
}
$var = $tmp;
return $ret;
}
$another = ‘test’;
$testvar = ‘test’;
echo getvarname($testvar); //echoes ‘testvar’
?>

How to display how many weeks and days left when a start date and an end date is given?

Following code in php displays how many weeks and days left when a start date and an end date is given:
function daysWeeks($start,$end)
{
$days_left=$end-$start;
$weeks = (int) ($days_left / 7) ;
$days = $days_left % 7;
echo “$weeks weeks and $days days left!”;
}
$start=”05/08/2008″;
$end=”25/08/2008″;
daysWeeks($start,$end);

Code To determine the number of hits in your website :

1.Create a .txt file, say hit.txt.
2. Enter the value 0 in hit.txt and save.
3. Create a php file, say counter.php with the following code…