PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Martin   Text to Timestamp   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: Text to Timestamp
Convert English expressions into time values
Author: By
Last change:
Date: 14 years ago
Size: 4,253 bytes
 

Contents

Class file image Download
<?php
require('Time_When.php');

$obj = new Time_When();

$obj->addType('-3months', array('time' => -(60*60*60*24*30*3), 'text' => '1 quarter ago'));
$obj->addType('-6months', array('time' => -(60*60*60*24*30*6), 'text' => '2 quarters ago'));
$obj->addType('-9months', array('time' => -(60*60*60*24*30*9), 'text' => '3 quarters ago'));
// If you dont use $obj->textAssigner in the text field, you dont need to add assignments

$obj->addType('-xmonths', array('time' => -(60*60*60*24*30*3), 'text' => '1 quarter ago'))
    ->
addType('-xxmonths', array('time' => -(60*60*60*24*30*6), 'text' => '2 quarters ago'))
    ->
addType('-xxxmonths', array('time' => -(60*60*60*24*30*9), 'text' => '3 quarters ago'));
// Can also be typed in oneline

$obj->addType(
           
'yearexample', // The unique identifier name
           
array(
               
'time' => -(60*60*60*24*30*12),
               
// Here we add our time search (REMEMBER - if its less than...
               
'text' => $obj->textAssigner . ' ' . $obj->textAssigner . ' ago',
               
// Here we assign some text, $obj->textAssigner will be replaced by our assign array
               
'assign' => array(
                               
// Now to our assigns, as we have used $obj->textAssigner 2 times, we also need to items in this array
                               
$obj->textReplacer,
                               
// The first $obj->textAssigner we will replace by our replace sign (which will be calculated by 'time')
                               
array(EQUALS, 1, 'year', 'years')
                               
// Here we have a if statement, it says
                                // If 1 == $obj->textReplacer then replace $obj->textAssigner with year, else replace $obj->textAssigner with years
                           
)
            )
        );


$obj->removeType('year');
// Add the unique identifier



$obj = new Time_When();

$obj->addTimer(time()-60*60*60, time(), '1 hour ago');
// Here we add a new timer,

$array = $obj->getResult();
var_dump( $array );
/*
array
  '1 hour ago' =>
    array
      'starttimer' => int 1254375267
      'endtimer' => int 1254162867
      'timer' => int -212400
      'result' =>
        array
          'converter' =>
            array
              '-min' =>
                array
                  'time' => int -3600
                  'text' => string '%s %s ago' (length=9)
                  'assign' =>
                    array
                      0 => string '[NUM]' (length=5)
                      1 =>
                        array
                          0 => string '<' (length=1)
                          1 => int 2
                          2 => string 'minute' (length=6)
                          3 => string 'minutes' (length=7)
                  'assignments' =>
                    array
                      0 => string '59' (length=2)
                      1 => string 'minutes' (length=7)
          'translated' => string '59 minutes ago' (length=14)
*/

// This array will be outputted, so we can both debug and other stuff

// But instead of using getResult() we can use __toString()
$string = $obj->__toString();
var_dump( $string );
/*
string '59 minutes ago' (length=14)
*/

// If we had several timers, we would get an array in __toString() example
$obj->addTimer(time()-60*60*60, time());
$obj->addTimer(time()-60*60*60, time(), 'testing #1');
$obj->addTimer(time()-60*60*60, time());
$obj->addTimer(time()-60*60*60, time(), 'testing #2');
$string = $obj->__toString();
var_dump( $string );
/*
array
  '1 hour ago' => string '1 hour ago' (length=10)
  0 => string '1 hour ago' (length=10)
  'testing #1' => string '1 hour ago' (length=10)
  1 => string '1 hour ago' (length=10)
  'testing #2' => string '1 hour ago' (length=10)
*/

// The fastest way to get a single will be
$obj = new Time_When();
echo
$obj->addTimer(time()-60*60*60);
// You dont need to invoke __toString() because this is magic function in PHP

// After __toString() has been invoked all timers will be removed!
// This will NOT happen after getResult()



$obj = new Time_When();

$obj->removeTimer();
// This will remove ALL timers!
// You can also echo this to see how many timers there have been removed

$obj->removeTimer('1 hour ago');
// This will remove the timer with id '1 hour ago'
// You can also echo this to see if the timer has removed one.