Jump to content

[SOLVED] Extract a date from a screen


ainoy31

Recommended Posts

Hello-

 

I need help with scraping data from the screen.  Here is what the data looks like:

 

Status: DELIVERED 10/17/07. 

 

I can get the DELIVERED word out with no problem.  Also, I need to get the date out as well.  Here is my code:

 

if(stristr($line, 'Status:'))

{

    $status_line = $line_number; 

}

.

.

.

if(isset($status_line) and $status_line + 1 == $line_number)

{

if(stristr($line, 'Delivered'))

{

$delivered = true;

$status = 'Delivered';

}

else

{

$delivered = false;

$status = trim(strip_tags($line));

$deliv_date = null;

}

 

I am stuck on it going pass the DELIVERED to get me date out.  Hope this is clear enough.  Much appreciation.

Link to comment
https://forums.phpfreaks.com/topic/73667-solved-extract-a-date-from-a-screen/
Share on other sites

Thanks for the reply.  It is almost working.  Here is what is going on now.  The date on the screen is 10/11/07.

 

$pattern = "/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/";

preg_match($pattern, $line, $matches);

print_r($matches);

$date = trim( strip_tags( $matches[1] ) );

$date = substr( $date, 0, 6 ) . '20' . substr( $date, 6, 2 );

$deliv_date = date("Y-m-d", strtotime( $date ) );

$status = 'Delivered';

$delivered = true;

 

The print out is Array ( [0] => 10/11/07 [1] => 10 [2] => 11 [3] => 07 ).  I am trying to convert to the y-m-d format.  When I echo $deliv_date, I get 2007-10-17 instead of 2007-10-11.  Trying to figure out why it is adding 6 to the day.

assuming there are no dates in the past, nor beyond 2099...

 

$pattern = "/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/";
preg_match($pattern, $line, $matches);
$date_string = "20".$matches[3]."-".$matches[1]."-".$matches[2];

 

but if they are, i guess i'd shortcut to:

 

$deliv_date = date("Y-m-d", strtotime( $matches[0] ) );

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.