Skip to content

Instantly share code, notes, and snippets.

@byronrode
Created March 30, 2011 11:16
Show Gist options
  • Save byronrode/894221 to your computer and use it in GitHub Desktop.
Save byronrode/894221 to your computer and use it in GitHub Desktop.
This function generates and returns static time remaining for Morph Demo refresh, based on current server time. To customize the output, edit the $time_remaining string.
<?php
function get_time_remaining($duration_in_hours){
$timestamp = time();
$current_minutes = date('i', $timestamp);
$current_seconds = date('s', $timestamp);
// pluralize hours, minutes and seconds
(($duration_in_hours - 1) == 1) ? $hours = ' hour ' : $hours = ' hours ';
((60 - $current_minutes) <= 01) ? $minutes = ' minute ' : $minutes = ' minutes ';
((60 - $current_seconds) <= 1) ? $seconds = ' second' : $seconds = ' seconds';
if($duration_in_hours <= 1){
// one hour
$time_remaining = 'This demo will be refreshed in <strong><span id="cd_mins">'.(60 - $current_minutes).'</span> '.$minutes.' <span id="cd_seconds">'.(60 - $current_seconds).'</span> '.$seconds.'</strong>.</span>';
}else{
// one hour and upwards.
$time_remaining = 'This demo will be refreshed in <strong><span id="cd_hours">'.($duration_in_hours - 1).'</span> '.$hours.' <span id="cd_mins">'.(60 - $current_minutes).'</span> '.$minutes.' <span id="cd_seconds">'.(60 - $current_seconds).'</span> '.$seconds.'</strong>.</span>';
}
return $time_remaining;
}
?>
<!-- Usage -->
<?php echo get_time_remaining(1); ?><br />
<?php echo get_time_remaining(2); ?><br />
<?php echo get_time_remaining(3); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment