Login   Register  
PHP Classes
elePHPant
Icontem

File: LITERAL_TIME.class

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bryan Smith  >  Literal Time  >  LITERAL_TIME.class  >  Download  
File: LITERAL_TIME.class
Role: Class source
Content type: text/plain
Description: Main Literal Time Object
Class: Literal Time
Spell time with English words
Author: By
Last change: Error when displaying numbers that end in 0. This has been corrected.
Date: 7 years ago
Size: 7,356 bytes
 

Contents

Class file image Download
<?php
/**
 * Display the time in a text format
 *
 * Usage:
 * $Time = new LITERAL_TIME();
 * echo $Time->Time();
 *
 * To display current time without Meridiem:
 * $Time = new LITERAL_TIME(null, false);
 * echo $Time->Time();
 *
 * Author: Bryan Smith (http://smokeyb.com)
 */
class LITERAL_TIME
{
    const 
MIDNIGHT          'Midnight';
    const 
NOON              'Noon';
    const 
QUARTER_TO        'Quarter To';
    const 
QUARTER_AFTER     'Quarter After';
    const 
AT_NIGHT          'at Night';
    const 
IN_THE_EVENING    'in the Evening';
    const 
IN_THE_MORNING    'in the Morning';
    const 
IN_THE_AFTERNOON  'in the Afternoon';

    
/**
     * The text version of the hour
     *
     * @var String
     */
    
private $Hour_;
    
/**
     * The text version of the minues
     *
     * @var String
     */
    
private $Minute_;
    
/**
     * The text version of the meridiem
     *
     * @var String
     */
    
private $Meridiem_;
    
/**
     * Display the meridiem text or not.
     *
     * @var Boolean
     */
    
private $DisplayMeridiem_;
    
/**
     * Determines if hours or minutes needs to be displayed first.
     *
     * @var unknown_type
     */
    
private $DisplayHourFirst_ true;
    
/**
     * Array of number to text translations
     *
     * @var Array
     */
    
private $NumberLiterals_ = array
        (
            
1       => 'One',
            
2       => 'Two',
            
3       => 'Three',
            
4       => 'Four',
            
5       => 'Five',
            
6       => 'Six',
            
7       => 'Seven',
            
8       => 'Eight',
            
9       => 'Nine',
            
10      => 'Ten',
            
11      => 'Eleven',
            
12      => 'Twelve',
            
13      => 'Thirteen',
            
14      => 'Fourteen',
            
15      => 'Fifteen',
            
16      => 'Sixteen',
            
17      => 'Seventeen',
            
18      => 'Eighteen',
            
19      => 'Nineteen',
            
20      => 'Twenty',
            
30      => 'Thirty',
            
40      => 'Fourty',
            
50      => 'Fifty',
        );

    public function 
__construct($TimeStamp null$DisplayMeridiem true)
    {
        
$DateTime = (is_null($TimeStamp) || empty($TimeStamp)) ? date('h:i:a') : date('h:i:a'$TimeStamp);
        list(
$Hour$Minute$Meridiem) = split(':'$DateTime);
        
$this->GetTimeText_($Hour$Minute$Meridiem);
        
$this->DisplayMeridiem_ $DisplayMeridiem;
    }

    
/**
     * Displays the time in a text format
     *
     * @param Integer $TimeStamp
     * @return String
     */
    
public function Time($TimeStamp null)
    {
        if (!
is_null($TimeStamp))
            
$this->__construct($TimeStamp);

        if (
$this->Minute_)
        {
            if (
$this->DisplayHourFirst_ == true)
            {
                
$Time $this->Hour_ ' ' $this->Minute_;
            }
            else
            {
                if (
$this->Minute_ == self::QUARTER_TO || $this->Minute_ == self::QUARTER_AFTER)
                    
$Time $this->Minute_ ' ' $this->Hour_;
                else
                    
$Time $this->Minute_ ' after ' $this->Hour_;
            }
        }
        else
            
$Time $this->Hour_;

        if (
$this->DisplayMeridiem_ == true)
            
$Time .= ' ' $this->Meridiem_;

        return 
$Time '.';
    }

    
/**
     * Assembles the time into text format
     *
     * @param Mixed $Hour
     * @param Mixed $Minute
     * @param String $Meridiem
     * @return String
     */
    
private function GetTimeText_($Hour$Minute$Meridiem)
    {
        
$this->GetHourText_($Hour$Minute$Meridiem);
        if (
$Hour == 12 && ($Minute == 15 || $Minute == 45))
            
$this->GetMinuteText_($Hour$Minute$Meridiem);
        else
        {
            
$this->GetMinuteText_($Hour$Minute$Meridiem);
            
$this->GetMeridiemText_($Hour$Minute$Meridiem);
        }
    }

    
/**
     * Converts the number value of the hour into its text verion
     *
     * @param Mixed $Hour
     * @param Mixed $Minute
     * @param String $Meridiem
     * @return String
     */
    
private function GetHourText_($Hour$Minute$Meridiem)
    {
        
// Set time to next meridiem if hour is past 12.
        
if ($Hour 12)
        {
            
$Hour 1;
            
$Meridiem $Meridiem == 'am' 'pm' 'am';
        }

        if (
$Hour == 12 && ($Minute == '00' || $Minute == 15 || $Minute == 45))
        {
            if (
$Meridiem == 'am')
                
$this->Hour_ self::MIDNIGHT;
            else
                
$this->Hour_ self::NOON;
        }
        else
            
$this->Hour_ $this->NumToStr_($Hour);
    }


    
/**
     * Converts the number value of the meridiem into its text verion
     *
     * @param Mixed $Minute
     */
    
private function GetMinuteText_($Hour$Minute$Meridiem)
    {
        if (
$Minute 0)
        {
            switch (
$Minute)
            {
                case 
15:
                    
$this->Minute_ self::QUARTER_AFTER;
                    
$this->DisplayHourFirst_ false;
                    break;

                
// Move ahead to the next hour to display quarter to.
                
case 45:
                    
$this->GetHourText_($Hour 1$Minute$Meridiem);
                    
$this->Minute_ self::QUARTER_TO;
                    
$this->GetMeridiemText_($Hour$Minute$Meridiem);
                    
$this->DisplayHourFirst_ false;
                    break;

                default:
                    if (
$Minute 10)
                        
$this->DisplayHourFirst_ false;

                    
$this->Minute_ $this->NumToStr_($Minute);
                    break;
            }
        }
    }

    
/**
     * Converts the number value of the meridiem into its text verion
     *
     * @param Mixed $Hour
     * @param Mixed $Minute
     * @param String $Meridiem
     */
    
private function GetMeridiemText_($Hour$Minute$Meridiem)
    {
        if (
$Meridiem == 'am')
            
$this->Meridiem_ self::IN_THE_MORNING;
        else
        {
            if (
$Hour 6)                        // According to Wikipedia, afternoon is before 6;
                
$this->Meridiem_ self::IN_THE_AFTERNOON;
            elseif (
$Hour 9)                        // According to Wikipedia, evening is before 9;
                
$this->Meridiem_ self::IN_THE_EVENING;
            else
                
$this->Meridiem_ self::AT_NIGHT;
        }
    }

    
/**
     * Converts a number to a text value
     *
     * @param Integer $Number
     * @return String
     */
    
private function NumToStr_($Number)
    {
        if (
$Number 20)
        {
            if (
$Number[1] > 0)
                return 
$this->NumberLiterals_[str_pad($Number[0], 2'0')] . '-' $this->NumberLiterals_[$Number[1]];
            else
                return 
$this->NumberLiterals_[str_pad($Number[0], 2'0')];
        }
        else
            return 
$this->NumberLiterals_[ltrim($Number'0')];
    }
}
?>