Skip to content

Instantly share code, notes, and snippets.

@thomaslarsson
Created July 18, 2013 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaslarsson/6029616 to your computer and use it in GitHub Desktop.
Save thomaslarsson/6029616 to your computer and use it in GitHub Desktop.
DateRange
<?php
// Make sure direct access is not allowed
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Allows you to calculate the remainder of time from now to a given DateTime
*
* @author Thomas Maurstad Larsson, Logoz As <thomas@logoz.no>
* @link http://nybilguiden.no
* @copyright Copyright (c) 2012
*/
class DateTimeDifference
{
/** @var array Translation of difference between singular and plural in Norwegian */
public static $translation = array
(
"day" => array("dag", "dager"),
"hrs" => array("time", "timer"),
"min" => array("minutt", "minutter"),
"sec" => array("sekund", "sekunder"),
"exp" => "Utgått",
);
private static $is_valid = null;
private static $seconds = 0;
private static $minutes = 0;
private static $hours = 0;
private static $days = 0;
/**
* Returns the dateTime difference between a set timestamp and now.
*
* @param String $date A date in the future
* @return boolean Trie on successful calculation, else false
*/
public static function calculate( $date )
{
try
{
date_default_timezone_set('Europe/Oslo');
// Get datetimes
$now = new DateTime(date("Y-m-d"));
$date = new DateTime($date);
// Make sure date is valid
if ( self::is_valid($now, $date) )
{
// Get a DateInterval object and set difference
$dateInterval = $now->diff($date);
// Set difference
self::$days = $dateInterval->days;
self::$hours = $dateInterval->h;
self::$minutes = $dateInterval->i;
self::$seconds = $dateInterval->s;
// Sucessful calculation
return true;
}
return false;
}
catch (Exception $e)
{
return false;
}
}
/**
* Returns the number of days remaining, following a
* calculation of the difference between two dateTimes.
*
* @return int The number of days remaining
*/
public static function get_days()
{
if ( self::$days > 0 )
{
return self::$days;
}
return false;
}
/**
* Returns the number of hours remaining, following a
* calculation of the difference between two dateTimes.
*
* @return int The number of hours remaining
*/
public static function get_hours()
{
if ( self::$hours > 0 )
{
return self::$hours;
}
return false;
}
/**
* Returns the number of minutes remaining, following a
* calculation of the difference between two dateTimes.
*
* @return int The number of minutes remaining
*/
public static function get_minutes()
{
if ( self::$minutes > 0 )
{
return self::$minutes;
}
return false;
}
/**
* Returns the number of seconds remaining, following a
* calculation of the difference between two dateTimes.
*
* @return int The number of seconds remaining
*/
public static function get_seconds()
{
if ( self::$seconds > 0 )
{
return self::$seconds;
}
return false;
}
public static function get_round_percentage( $start, $end )
{
// Set datetimes
$start = new DateTime($start);
$now = new DateTime(date("Y-m-d"));
$end = new DateTime($end);
if ( $end <= $now )
{
return 0;
}
// Calculate number of days total
$total_days = $start->diff($end);
$total_days = $total_days->format('%a') + $total_days->format('%h')/24; // Days, plus hours divided by 24 (a day in hours)
// Calculate number of days remaining
$remaining_days = $now->diff($end);
$remaining_days = $remaining_days->format('%a') + $remaining_days->format('%h')/24;
// Calculate how much a day is in percentage
$percentage = 100 / $total_days;
// Calculate how many percentage (days) remains, and round down
return floor($remaining_days * $percentage);
}
/**
* Returns the time difference as human-readable text
*
* @return String A difference as text
*/
public static function get_human_diff( )
{
// Make sure date is not expired
if ( self::$is_valid )
{
// Filter days
if ( self::get_days() > 0 ) {
return self::get_conjugation(self::$days, "day");
}
// Filter hours
if ( self::get_hours() > 0 ) {
return self::get_conjugation(self::$hours, "hrs");
}
// Over 10 minutes
if ( self::get_minutes() >= 10 ) {
return self::get_conjugation(self::$minutes, "min");
}
// Under 10 minuttes
return
self::get_conjugation(self::$minutes, "min").' '.
self::get_conjugation(self::$seconds, "sec");
}
// Expired
return self::get_expired_message();
}
/**
* Will check if a given time difference is less than 0
*
* @return boolean True if dateTime is expired, else false
*/
private static function is_valid( DateTime $now, DateTime $comparision )
{
return self::$is_valid = ($now < $comparision);
}
/**
* Returns the expired message set in the translation array
*
* @return String The message defining an expired date.
*/
private static function get_expired_message()
{
return self::$translation['exp'];
}
/**
* Returns singular or plural version of words.
*
* @param mixed $time A time as an int
* @param String $key The key in the translation array
*/
private static function get_conjugation($time, $key)
{
return ($time === 1) ? $time.' '.self::$translation[$key][0] : $time.' '.self::$translation[$key][1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment