Month: August 2016

How to calculate “time ago” in PHP

function time_ago($time)
{
$periods = array(“second”, “minute”, “hour”, “day”, “week”, “month”, “year”, “decade”);
$lengths = array(“60″,”60″,”24″,”7″,”4.35″,”12″,”10”);

$now = time();

$difference = $now – $time;
$tense = “ago”;

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
$periods[$j].= “s”;
}
return “$difference $periods[$j] ago”;
}
$time = strtotime(your date);
echo time_ago($time);

Output:-
1 minute ago

Sending files using cURL in PHP

index.php

$target_url = ‘http://example.com/post.php ‘;
$file_name_with_full_path = “C:/xampp/htdocs/demo/1.jpg”; //full path of your file

$ch = curl_init();
$args[‘files’] = new CURLFile($file_name_with_full_path);

curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);

Post.php

Array
(
[files] => Array
(
[name] => 1.jpg
[type] => application/octet-stream
[tmp_name] => C:\xampp\tmp\php4565.tmp
[error] => 0
[size] => 158530
)

)