We can add number of of days, months, years, hours and seconds with a DateTime object.
Example
$date = new DateTime(‘2007-05-05’);
$date->add(new DateInterval(‘P5Y’));
echo $date->format(‘Y-m-d’) . “n”;
?>
Output
2012-05-05
We can add number of of days, months, years, hours and seconds with a DateTime object.
Example
Output
2012-05-05
Example
<?php
$date = new DateTime(“24-May-2011 20:15:22”);
echo $date->format(“d-m-Y H:i:s”).'<br />’;
date_add($date, new DateInterval(“P1Y”));
echo ‘<br />’.$date->format(“d-m-Y”).’ : I Year’;
?>
Output
24-05-2011 20:15:22
24-05-2012 : I Year
It checks whether the date is valid or not
General Format :checkdate ( int $month , int $day , int $year )
Example :
<?php
if(checkdate(12, 31, 2000))
echo “Given Date Is Correct”;
?>
Output
Given Date Is Correct
<?php echo rand(1, 10) ?>
We can find out the position using the function strpos() that will return the position of a character
Example
<?php
$string=”phpcodez.com”;
echo strpos($string,”.”) // 8
?>
<?php echo get_magic_quotes_gpc()?”Enabled”:”Disabled”; ?>
A function is a group of statements that will do certain tasks.
Why Function
When we develop a module in a project , we may need to implement certain tasks(block of statements ) more than once . Writing same block of codes more than once is not at all a good practice . Instead we can give a name to that block and can use them whenever necessary . When we define a block with a name , it is known as function .
• Blocks of code that execute in isolation (and local scope) that perform an action
• Function names are case-insensitive; defined in global scope
• Can be referenced before being defined unless function conditional
• Types: built-in (php supplied); user-defined; externally provided
General Format
function funation_name(){
echo “Error”; // can be on or more lines of statements
}
function – a keyword
funation_name – Any desired name(Should be meaningful)
Once the function is desfined , You can can invoke it by calling its name
<?php funation_name(); ?> // It will print the text “ Error”
A function can have arguments . When a fuction is define using arguments in it , we should pass the values when calling the it .
Example :
function function_sum($a,$b){
echo $a+$b;
}
<?php function_sum(7,8); ?> // It will display 15 as the result .
Note : We should not use any Builtin function name as the function name
A function is a group of statements that can be executed any time you want.
PHP has a number of predefine functions for you to use.
Below given are the different type of funtions available in PHP