maybe this a factory design pattern

followup to that great answer, but adding new requirements

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28236754.html




I read about factory design pattern


there are more input types: but want the same type of output
(07/17 15:16)
(08/04 16:27)
(08/04 02:24)
(08/04 02:31)
(07/17 15:28)
(06/27 00:40)
(06/27 00:26)
(06/27 00:26)
(06/26 21:46)
1 month ago

but want these existing inputs to return time values also
(1 min ago)
(17 mins ago)
(1 hr ago)

(12 hrs ago)
1 day ago
2 days ago
1 week ago


I do not want to do an if statement outside of the class
if word contains 'ago' then go to class1
else go to class2


I am not an expert: factory design pattern may not be the best option.
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I don't think of this as being a factory; it's more of a data normalization process.  I'll try to give you some examples in a moment.  While I am doing that look up the "Gang of Four" and find out about their book on Design Patterns.
0
Ray PaseurCommented:
It looks like you're really getting it about the importance of test data!  That's something that will make your work much faster and more accurate.

Please see: http://www.laprbass.com/RAY_temp_rgb192.php

<?php // RAY_temp_rgb192.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28238054.html


// TEST DATA
$lapse = array
( '(1 min ago)'
, '(17 mins ago)'
, '(1 hr ago)'
, '(12 hrs ago)'
, '1 day ago'
, '2 days ago'
, '1 week ago'
, 'Bozo'
, '(07/17 15:16)'
, '(08/04 16:27)'
, '(08/04 02:24)'
, '(08/04 02:31)'
, '(07/17 15:28)'
, '(06/27 00:40)'
, '(06/27 00:26)'
, '(06/27 00:26)'
, '(06/26 21:46)'
, '1 month ago'
, '(1 min ago)'
, '(17 mins ago)'
, '(1 hr ago)'
, '(12 hrs ago)'
, '1 day ago'
, '2 days ago'
, '1 week ago'
)
;

Class MyDateTime extends Datetime
{
    // OVERRIDE THE CONSTRUCTOR METHOD TO TAILOR OUR INPUT STRINGS
    public function __construct($str)
    {
        // SAVE THE INITIAL STRING
        $this->s = $str;

        // REMOVE UNWANTED CHARACTERS
        $str = str_replace('(', NULL, $str);
        $str = str_replace(')', NULL, $str);

        // TRANSLATE TO 'HOUR'
        $str = str_replace('hr', 'hour', $str);

        // CALL THE PARENT CONSTRUCTOR
        try
        {
            $this->e = NULL;
            parent::__construct($str);
        }

        // CATCH THE EXCEPTION MESSAGE
        catch (Exception $e)
        {
            $this->e = $e->getMessage();

            // OPTIONAL - SHOW MESSAGE?
            // echo $this->e;
        }
    }

    // OVERRIDE THE FORMAT METHOD TO MAKE A MORE MEANINGFUL ERROR MESSAGE
    public function format($pattern)
    {
        if ($this->e) return $this->s . ': ' . $this->e;
        return parent::format($pattern);
    }
}


// MAN PAGE: http://php.net/manual/en/class.datetime.php
foreach ($lapse as $str)
{
    echo PHP_EOL . "$str => ";
    $dat = new MyDateTime($str);
    echo $dat->format('Y-m-d H:i:s');
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
time works without any changes in the code

thanks
0
Ray PaseurCommented:
Yeah, strtotime() and DateTime are pretty smart!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.