note 102881 modified in function.time by danbrown
| From: | danbrown@php.net | Date: | Fri, 11 Mar 2011 23:51:25 +0000 |
| Subject: | note 102881 modified in function.time by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177810@lists.php.net to get a copy of this message | ||
My take on a function to find the differences between a timestamp and current time.
Format: findTime($sometime['stamp'], '%d Days, %h Hours, %m Minutes');
Always use plural it will auto correct on singular results. You don't have to include all
%d,%m,%h you may include only one. To get Total Hours remaining(including days) use %ho. To get
Total Minutes remaining(including hours and days) use %mo. Take a look at the format I assumed to
make any changes.
<?php
function findTime($timestamp, $format) {
$difference = $timestamp - time();
if($difference < 0)
return false;
else{
$min_only = intval(floor($difference / 60));
$hour_only = intval(floor($difference / 3600));
$days = intval(floor($difference / 86400));
$difference = $difference % 86400;
$hours = intval(floor($difference / 3600));
$difference = $difference % 3600;
$minutes = intval(floor($difference / 60));
if($minutes == 60){
$hours = $hours+1;
$minutes = 0;
}
if($days == 0){
$format = str_replace('Days', '?', $format);
$format = str_replace('Ds', '?', $format);
$format = str_replace('%d', '', $format);
}
if($hours == 0){
$format = str_replace('Hours', '?', $format);
$format = str_replace('Hs', '?', $format);
$format = str_replace('%h', '', $format);
}
if($minutes == 0){
$format = str_replace('Minutes', '?', $format);
$format = str_replace('Mins', '?', $format);
$format = str_replace('Ms', '?', $format);
$format = str_replace('%m', '', $format);
}
$format = str_replace('?,', '', $format);
$format = str_replace('?:', '', $format);
$format = str_replace('?', '', $format);
$timeLeft = str_replace('%d', number_format($days), $format);
$timeLeft = str_replace('%ho', number_format($hour_only), $timeLeft);
$timeLeft = str_replace('%mo', number_format($min_only), $timeLeft);
$timeLeft = str_replace('%h', number_format($hours), $timeLeft);
$timeLeft = str_replace('%m', number_format($minutes), $timeLeft);
if($days == 1){
$timeLeft = str_replace('Days', 'Day', $timeLeft);
$timeLeft = str_replace('Ds', 'D', $timeLeft);
}
if($hours == 1 || $hour_only == 1){
$timeLeft = str_replace('Hours', 'Hour', $timeLeft);
$timeLeft = str_replace('Hs', 'H', $timeLeft);
}
if($minutes == 1 || $min_only == 1){
$timeLeft = str_replace('Minutes', 'Minute', $timeLeft);
$timeLeft = str_replace('Mins', 'Min', $timeLeft);
$timeLeft = str_replace('Ms', 'M', $timeLeft);
}
return $timeLeft;
}
}
?>
--was--
My take on a function to find the differences between a timestamp and current time.
Format: findTime($sometime['stamp'], '%d Days, %h Hours, %m Minutes');
Always use plural it will auto correct on singular results. You don't have to include all
%d,%m,%h you may include only one. To get Total Hours remaining(including days) use %ho. To get
Total Minutes remaining(including hours and days) use %mo. Take a look at the format I assumed to
make any changes.
function findTime($timestamp, $format) {
$difference = $timestamp - time();
if($difference < 0)
return false;
else{
$min_only = intval(floor($difference / 60));
$hour_only = intval(floor($difference / 3600));
$days = intval(floor($difference / 86400));
$difference = $difference % 86400;
$hours = intval(floor($difference / 3600));
$difference = $difference % 3600;
$minutes = intval(floor($difference / 60));
if($minutes == 60){
$hours = $hours+1;
$minutes = 0;
}
if($days == 0){
$format = str_replace('Days', '?', $format);
$format = str_replace('Ds', '?', $format);
$format = str_replace('%d', '', $format);
}
if($hours == 0){
$format = str_replace('Hours', '?', $format);
$format = str_replace('Hs', '?', $format);
$format = str_replace('%h', '', $format);
}
if($minutes == 0){
$format = str_replace('Minutes', '?', $format);
$format = str_replace('Mins', '?', $format);
$format = str_replace('Ms', '?', $format);
$format = str_replace('%m', '', $format);
}
$format = str_replace('?,', '', $format);
$format = str_replace('?:', '', $format);
$format = str_replace('?', '', $format);
$timeLeft = str_replace('%d', number_format($days), $format);
$timeLeft = str_replace('%ho', number_format($hour_only), $timeLeft);
$timeLeft = str_replace('%mo', number_format($min_only), $timeLeft);
$timeLeft = str_replace('%h', number_format($hours), $timeLeft);
$timeLeft = str_replace('%m', number_format($minutes), $timeLeft);
if($days == 1){
$timeLeft = str_replace('Days', 'Day', $timeLeft);
$timeLeft = str_replace('Ds', 'D', $timeLeft);
}
if($hours == 1 || $hour_only == 1){
$timeLeft = str_replace('Hours', 'Hour', $timeLeft);
$timeLeft = str_replace('Hs', 'H', $timeLeft);
}
if($minutes == 1 || $min_only == 1){
$timeLeft = str_replace('Minutes', 'Minute', $timeLeft);
$timeLeft = str_replace('Mins', 'Min', $timeLeft);
$timeLeft = str_replace('Ms', 'M', $timeLeft);
}
return $timeLeft;
}
}
http://php.net/manual/en/function.time.php