add (just now) to array of data

(just now) = current datetime

add this to the array of data


<?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

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:
In PHP there is the value of date() and time() called without any time-setting arguments.  It just depends on how you want to format the information.

<?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_28256839.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'
)
;

// ADD JUST-NOW
$lapse[] = date('c');

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
rgb192Author Commented:
I am looking to add a literal
'(just now)'

<?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_28256839.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'
, '(just now)'
)
;

// ADD JUST-NOW
$lapse[] = date('c');

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





(1 min ago) => 2013-10-03 09:48:43
(17 mins ago) => 2013-10-03 09:32:43
(1 hr ago) => 2013-10-03 08:49:43
(12 hrs ago) => 2013-10-02 21:49:43
1 day ago => 2013-10-02 09:49:43
2 days ago => 2013-10-01 09:49:43
1 week ago => 2013-09-26 09:49:43
Bozo => Bozo: DateTime::__construct() [datetime.--construct]: Failed to parse time string (Bozo) at position 0 (B): The timezone could not be found in the database
(07/17 15:16) => 2013-07-17 15:16:00
(08/04 16:27) => 2013-08-04 16:27:00
(08/04 02:24) => 2013-08-04 02:24:00
(08/04 02:31) => 2013-08-04 02:31:00
(07/17 15:28) => 2013-07-17 15:28:00
(06/27 00:40) => 2013-06-27 00:40:00
(06/27 00:26) => 2013-06-27 00:26:00
(06/27 00:26) => 2013-06-27 00:26:00
(06/26 21:46) => 2013-06-26 21:46:00
1 month ago => 2013-09-03 09:49:43
(1 min ago) => 2013-10-03 09:48:43
(17 mins ago) => 2013-10-03 09:32:43
(1 hr ago) => 2013-10-03 08:49:43
(12 hrs ago) => 2013-10-02 21:49:43
1 day ago => 2013-10-02 09:49:43
2 days ago => 2013-10-01 09:49:43
1 week ago => 2013-09-26 09:49:43
(just now) => (just now): DateTime::__construct() [datetime.--construct]: Failed to parse time string (just now) at position 0 (j): The timezone could not be found in the database
2013-10-03T09:49:43-04:00 => 2013-10-03 09:49:43
0
hieloCommented:
try:
...
        // REMOVE UNWANTED CHARACTERS
        $str = str_replace('(', '', $str);
        $str = str_replace(')', '', $str);

        // TRANSLATE TO 'HOUR'
        $str = str_replace('hr', 'hour', $str);
       $str = str_replace('just now', 'now', $str);
...
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
Ray PaseurCommented:
The details of what PHP can translate to a UNIX timestamp are available here:
http://php.net/manual/en/datetime.formats.php

Follow the links for explanations and examples.  

If you need to consider case-insensitivity, three is str_ireplace().  If you add very much to this list of novel expressions, be careful of the order that you use for replacement so there are no collisions in cascading replacements.
0
rgb192Author Commented:
I like the
 $str = str_replace('just now', 'now', $str);

solution and read about datetime.formats and str_ireplace()

Thanks
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.