Wednesday, 16 October 2013

php using date and time for various purposes

We always need to deal with date and time in our programming specially when we are using php .
time() and date() these two functions are very important regarding our concern .

see following line of php code :-
$a = time();
time() function returns total number of seconds passed till date from 1st jan 1970 .
date() function is used to get date formatted according to our wish .
function date() can have two parametes:-
1) format of time stamp that we want to show
2) optional:- timestamp which we need to use to get date

first parameter is string that shows that exact output format  . some characters are usefull like
Y -- to show current year
m -- to show current month number
d -- to show today's dat
H -- current hour   24 hour format
i -- to show current minute
s -- to get current seconds
t -- shows number of days in current month  or last date of current month


following are some uses date() function :-

echo  date(""); // this will print nothing
echo date("Y-m-d H:i:s"); // this will show complete time stamp i.e.  2013-10-16 12:00:00 as your system time

echo date("Y-9-d 12:00:s"); // this will show  2013-9-16 12:00:45

these were examples of formatting of time stamp that we want to show ;

second parameter is time stamp itself .
for example :-

 echo date("Y-m-d H:i:s" , time());  // this will show  current time
  echo date("Y-m-d H:i:s" , time() + 500);  // this will show  time after 500 seconds

  echo date("Y-m-d H:i:s" , time() + 86400 );  // this will show tomorrow's time because 86400 is number of seconds in one day .

if we want to see time 5 days ahead  then we need to do is
  echo date("Y-m-d H:i:s" , time()  + 5*86400);

similarly if we want to see time 5 days back then we need to do is
  echo date("Y-m-d H:i:s" , time()  - 5*86400);


if we need to find how many days are in this month or any month then we use "t" in format string (first parameter) .
like

$number_of days_this month = date("t"); // it will give only last date of the month

Thanks and please ask in comment if you need any clearification .

Tuesday, 15 October 2013

PHP file to return image or return 1x1 pixel image

Using php file we can return image .  there are many benefits of this thing . we can keep count that how many times our image is fetched or from which us/ip that image is fetched.also we can change image from php code without changing in the real html file .

see following code

<img src="http://localhost/test.php"/>

if we want that test.php should return any image
following is the code snippet to return that image

test.php :-

$file = file_get_contents("full path to ur image fle");
$contenttype = "gif" ; // it is type or extension of image like jpeg for jpeg images and gif for .gif images
header("Content-type: image/$contenttype");
echo $file;


we need to  take care of some thing in this .
we should not print or echo any other variable or string from the same php file .then it will be showing some error and image will not be returned .

IF we want to return 1x1 image pixel then following is the code snippet .

header('Content-Type: image/gif');
header('Content-Length: 43');
echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==');



this code will provide us 1x1 image 

Thursday, 8 August 2013

python check if variable is empty

This is very easy to know in python that if any variable is empty or not

try to use :

if not ur_var:
     print "variable is empty 

get attention towards   not   keyword . This does all things .

This is equivalent to empty($ur_var)  in php

not keyword in python also checks for lists (array) , objects , strings , integrers , floats etc ..

example :

a= 0  or a = "" or a ={} or a= []
if not a :
    print "empty"

in all cases it will print empty .

Thursday, 23 May 2013

linux set ulimit

for setting ulimit u need to run following command

ulimit -n  10000  for setting limit to 10000

for soft files  
ulimit -Sn  10000

and for hard files
ulimit -Hn 10000


do it individually for all users

and this setting will be vanished after system restart