Skip to content

Instantly share code, notes, and snippets.

@nilesolutions
Created December 23, 2017 11:41
Show Gist options
  • Save nilesolutions/87db43926105eb28cbeb5e1db2867fa1 to your computer and use it in GitHub Desktop.
Save nilesolutions/87db43926105eb28cbeb5e1db2867fa1 to your computer and use it in GitHub Desktop.
<?php
// date
$date = '423523463463'; // time stamp
$date = date("F j, Y, g:i a", $date);
// satring to date
$str = strtotime('10/16/2016');
$date = date('M d Y',$str);
echo $date;
// Count date diference
$now = time(); // or your date as well
$datediff = ($now) - ($item->created);
$days = floor($datediff / (60 * 60 * 24));
echo $days;
/**
* Time Ago Function
*/
function time_ago($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
echo time_ago($some_date);
/**
* CONVERT SECONDS TO DAYS, HOURS AND MINUTES
*
*/
function secsToStr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
$r.=$secs.' second';if($secs<>1){$r.='s';}
return $r;
}
/* usage */
$seconds = "56789";
$output = secsToStr($seconds);
echo $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment