0

I'm using PHP to read data from text file.

Here is the log data.

and the PHP:

$fileAccept = file_get_contents("\\\\192.168.184.13\\Reports\\".$dModel['MODEL_NAME'].$source."\\Accept\\Accept_".$dDtl['MODEL_CODE']."_".$dateCode."_".$dDtl['TS_CODE'].".txt");

$linesAccept = explode("\n",$fileAccept);
$rowsintimespanAccept = 0;

for($i = $readRowAfter; $i < count($linesAccept); $i++)
{
    // if the fileAccept is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
    $dateobjAccept = DateTime::createFromFormat($createFromFormat, $linesAccept[$i]);

    // check if date is in your Timespan
    if($dateobjAccept < $toDateTime && $dateobjAccept > $fromDateTime)
    {
        $rowsintimespanAccept++; // count if in timespan
    }
}

for($i = 0; $i < $rowsintimespanAccept; $i++)
{
    //I need get date time here for each
}

That code is working OK.

But now I want to get each date time from text file.

Any clue how to do this?

enter image description here

2 Answers 2

1

try this after your $fileAccept:

$linesAccept = explode("\n", $fileAccept);
$readRowAfter = 9;
$createFromFormat = 'D, M j  Y H:i:s';
$outputDateFormat = 'Y-m-d H:i:s';
$inTimespan = [];

for($i = $readRowAfter; $i < count($linesAccept); $i++)
{
    $lineData = explode("\t", $linesAccept[$i]);

    if (!isset($lineData[0]) || !isset($lineData[1])) {
        continue;
    }

    $dateString = $lineData[0] . " " . $lineData[1];

    $dateObj = DateTime::createFromFormat($createFromFormat, $dateString);

    // check if date is in your Timespan
    if($dateobjAccept < $toDateTime && $dateobjAccept > $fromDateTime)
    {
        $inTimespan[] = $dateObj->format($outputDateFormat);
    }
}

foreach ($inTimespan as $date) {
    echo $date;
    echo '<br/>';
}

The $createFromFormat = 'D, M j Y H:i:s'; the j in this might be d, but we can't know right now, since you only gave double-digit days, so if it breaks in case first day is 01 - then change it to d

Sign up to request clarification or add additional context in comments.

Comments

0

Unless I have misunderstood then your requirement was to be able to capture portions of each line in the logfile and use that in some fashion? The easiest way ( perhaps ) would be to use list and explode as shown below..

$logfile='c:/temp/Accept_10039787_012619_Ts1.txt';

$lines=file( $logfile );

foreach( $lines as $index => $line ){
    if( $index >=10 ){
        if( !empty( $line ) ){

            /* explode the string on TAB, capture two elements of interest as variables */
            list( $date, $time, , , )=explode("\t",$line );

            /* create a DateTime object */
            $oDate=DateTime::createFromFormat('D, M j  Y H:i:s', sprintf('%s %s', $date, $time ) );

            /* do somethig with the DateTime object... */
            echo $oDate->format('Y-m-d H:i:s') . '<br />';
        }
    }
}

Outputs:

2019-01-26 00:00:09
2019-01-26 00:00:23
2019-01-26 00:00:37
2019-01-26 00:00:50
2019-01-26 00:01:04
2019-01-26 00:01:17
2019-01-26 00:35:33.... etc etc cont'd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.