date functions in php and cookie



time function 

returns the current as representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.

date function



date function is used tp get the current date or time of the server.

there are some formats those are can be used with date function :

d - Represents the day of the month (01 to 31)


<?php

echo"d - Represents the day of the month (01 to 31) :",date("d");

?>

m - Represents a month (01 to 12)

<?php

echo"<br><br>m - Represents the month (01 to 31) :",date("m");

?>

Y - Represents a year (in four digits)

<?php

echo"<br><br>Y - Represents the year (if year is 2012 ,so output will be 2012) :",date("Y");


echo"<br><br>y - Represents the year (if year is 2012 ,so output will be 12) :",date("y");


?>


l (lowercase 'L') - Represents the day of the week

<?php

echo "<br> l (lowercase 'L') - Represents the day of the week",date("l")

?>



H - 24-hour format of an hour (00 to 23)


<?php

echo"<br><br>h - 12-hour format of an hour (1 to 12) :",date("h");


echo"<br><br>H - 24-hour format of an hour (0 to 24) :",date("H");

?>

h - 12-hour format of an hour with leading zeros (01 to 12)

i - Minutes with leading zeros (00 to 59)

<?php

echo"<br><br>i - Minutes with leading zeros (00 to 59) :",date("i");

?>

s - Seconds with leading zeros (00 to 59)

<?php

echo"<br><br>s - Seconds with leading zeros (00 to 59) :",date("s");


?>

a - Lowercase Ante meridiem and Post meridiem (am or pm)


<?php

echo"<br><br>A - Capital case Ante meridiem and Post meridiem (am or pm) :",date("A");


echo"<br><br>a - lower case Ante meridiem and Post meridiem (am or pm) :",date("a");

?>


cookie

cookie is a small file that embeds on the clients browser by the server.

it is generally used to identify a  user.
 
syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

example:

cookie.php


<form action="admin.php" method="post" >
Username: 
        <input name="username" type="text" value="<?php if(isset($_COOKIE["username"])) { echo $_COOKIE["username"]; } ?>" >
Password: 
<input name="password" type="password" value="<?php if(isset($_COOKIE["password"])) { echo 
$_COOKIE["password"]; } ?>" >

<input type="checkbox" name="remember" /> Remember me

<input type="submit" value="Login">

</form>





admin.php


<a href="cookie.php"> Login </a> 

<?php


if(!empty($_POST["remember"])) 
{

setcookie ("username",$_POST["username"],time()+ 3600);
setcookie ("password",$_POST["password"],time()+ 3600);

echo "data is set";

}
else
{

    setcookie("username","");
    setcookie("password","");
    echo "data is not set";

}


?>







0 Comments