как получить дату ("Ymd") из файла DAT - PullRequest
1 голос
/ 24 августа 2011

Я хочу получить свою дату в формате ("Ymd")

.dat содержит следующие данные, здесь

 3rd column is date 
177|RC456456177|1314160971|129$

178|RC456456456|1314167971|177$

179|RC456456456|1314130971|157$

180|RC456456456|1314260971|147$

Я использую это для получения значения

if($_POST['ordernum']==="")
{

$blank="Blank text box detected";
} 

elseif (isset($_POST["ordernum"]))
{

    $file = 'order.dat';

    $searchfor = $_POST["ordernum"];

$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor,'/');

$pattern = "/^$pattern.*$/m";

if(preg_match_all($pattern, $contents, $matches)){
   $result = implode("", $matches[0]);

          $results = explode("|", $result);
          $settings->tid = $results[1];
          //echo $settings->tid;
        }
       else{
           $res="No result found";
          // echo  $res;
        }
    }

Ответы [ 3 ]

1 голос
/ 24 августа 2011
// iterate through the file manually, it actually will be faster than reading
// it into either a long string or an array.
$fl = fopen( $path, 'r' );
$searchfor = $_POST["ordernum"] . '$';
// explode will not be needed because of fgetcsv
// http://php.net/manual/en/function.fgetcsv.php
while( $line = fgetcsv( $fl, 0, '|' ) )
{
    // test the last value
    if( $line[3] == $searchfor ) 
    {
        $settings->tid = date( 'Y-m-d', $results[2]);
        break;
    }
}

Как примечание (это не сработает для моего примера), вам лучше всего избегать использования всех $ внутри строк в двойных кавычках в PHP в качестве лучшей практики.

1 голос
/ 24 августа 2011
<?php

    $file = fopen ("whatever.dat", "r");

    while ($line = fgets($file)) {
        $contents = explode('|', $line);
        print date('Y.m.d', $contents[2]).PHP_EOL;
    }

    fclose ($file);
?>

Это должно работать.НТН

0 голосов
/ 24 августа 2011

Исходя из кода, который вы разместили выше, это должно сработать.

$date = date('Y.m.d', $results[2])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...