如何从 dat 文件中获取日期(“Ymd”)

发布于 2024-12-01 08:53:11 字数 904 浏览 0 评论 0原文

我想以(“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;
        }
    }

I want to fetch my date in ("Y.m.d") format

.dat file contains the following data, here

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

178|RC456456456|1314167971|177$

179|RC456456456|1314130971|157$

180|RC456456456|1314260971|147$

I am using this to fetch the value

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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你拒绝所有暧昧 2024-12-08 08:53:11
<?php

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

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

    fclose ($file);
?>

那应该有效。华泰

<?php

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

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

    fclose ($file);
?>

That should work. HTH

心碎的声音 2024-12-08 08:53:11
// 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"] . '

请注意(这对我的示例不起作用),作为最佳实践,您通常应该转义 PHP 中双引号字符串内的所有 $

; // 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 中双引号字符串内的所有 $

// 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"] . '

As a note (this won't work for my example), you should generally escape all $ inside of double-quoted strings in PHP as a best practice.

; // 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; } }

As a note (this won't work for my example), you should generally escape all $ inside of double-quoted strings in PHP as a best practice.

孤独难免 2024-12-08 08:53:11

根据您上面发布的代码,这应该可以解决问题。

$date = date('Y.m.d', $results[2])

Based on the code you posted above, this should do the trick.

$date = date('Y.m.d', $results[2])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文