Link to home
Start Free TrialLog in
Avatar of edyonline
edyonline

asked on

Find dates in string using php

In php, how can I return all date field given a random string?

for example:
alsdkfjalskdfjlskdfjalsk09/01/2010alskdfjaliew09-01-2010alksjalksjalkjThursday, November 04, 2010 at 8:00 AM - Friday, November 05, 2010 at 4:00 PM

to return array of
09/01/2010
09/01/2010
11/04/2010
11/05/2010

Thanks
Avatar of Scott Madeira
Scott Madeira
Flag of United States of America image

Doesn't seem like it is a trivial problem.  The way I would approach it would be to construct a set of filters most likely using a combination of regular expressions and text substring searches.  You would have a filter for each possible date format you want to capture.  From there you would run the string through each filter and pull out the dates that match and add them to the array.

The trick is defining the list of acceptable date formats.
Avatar of hexer4u
I agree with smadeira, but I have to ask.
Why do you have such a garbled string? Maybe the solution is further up the road.
Whatever the source of this data, it needs to be changed (maybe to XML?) because there is no reasonable programming solution to the question.  Please have a look at the article here and see if it gives you any ideas about how to encapsulate the DATETIME information.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Install this and run it - you will instantly see the impracticality of trying to get dates from an unformatted string.  I will tinker with it a little more, but I really think the better solution is to get the data formatted in a predictable manner.
<?php // RAY_temp_edyonline.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;


// ISOLATE THE DATES IN THIS STRING
$str = 'alsdkfjalskdfjlskdfjalsk09/01/2010alskdfjaliew09-01-2010alksjalksjalkjThursday, November 04, 2010 at 8:00 AM - Friday, November 05, 2010 at 4:00 PM';


date_default_timezone_set('America/Chicago');

$txs = array();
$max = strlen($str);
$min = 10; // TRY TO AVOID CATCHING PARTIAL DATES
$ptr = 0;
$eod = $max - $min;

while (TRUE)
{
    // TEST FOR A DATE STRING
    $txt = substr($str,$ptr,$min);
    if ($ts = strtotime($txt))
    {
        // CAPTURE THE VALUE THAT PASSED strtotime()
        $txs['TS' . $ts] = $txt;
    }

    $ptr++;
    if ($ptr > $eod) break;
}
// SHOW THE ISOLATED DATES
print_r($txs);

Open in new window

Another example.  In this strategy we try to use the maximum length of the string as a DATETIME string, and remove any parts that form a valid DATETIME string.  If we discard the tiny fragments, we get closer to finding the four substrings that look visually like dates.  But the findings are a bit misleading,

y, November 05, 2010 at 4:00 PM = Friday 05th of November 2010 11:00:00 PM
w09-01-2010alksja = Saturday 09th of January 2010 4:00:00 AM
k09/01/2010alskdf = Tuesday 31st of August 2010 9:00:00 AM

Conclusion - fix the problem somewhere else by adding predictable delimiters around the dates in the string.  Best of luck with it, ~Ray

<?php // RAY_temp_edyonline.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;


// ISOLATE THE DATES IN THIS STRING
$str = 'alsdkfjalskdfjlskdfjalsk09/01/2010alskdfjaliew09-01-2010alksjalksjalkjThursday, November 04, 2010 at 8:00 AM - Friday, November 05, 2010 at 4:00 PM';


date_default_timezone_set('America/Chicago');

$txs = array();

while (strlen($str) > 9)
{
    $max = strlen($str);
	$ptr = 0;

    while ($ptr < $max)
    {
        $hit = FALSE;
        // TEST FOR A DATE STRING
        $txt = substr($str,$ptr,$max);
        if ($ts = strtotime($txt))
        {
            $hit = TRUE;
            // CAPTURE THE VALUE THAT PASSED strtotime()
            $txs['TS' . $ts] = $txt;

            // REMOVE THE VALUE THAT PASSED strtotime()
            $str = substr_replace($str, '', $ptr);
        }
        $ptr++;
    }
    if (!$hit) $str = substr($str, 0, -1);
}
// REMOVE THE FRAGMENTS
foreach ($txs as $key => $val)
{
    if (strlen($val) < 10) unset($txs[$key]);
}
// SHOW THE ISOLATED DATES
print_r($txs);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Madeira
Scott Madeira
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial