【问题标题】:What does Windows FILETIME contain? Trying to convert to a PHP date/timeWindows FILETIME 包含什么?尝试转换为 PHP 日期/时间
【发布时间】:2009-03-04 18:22:02
【问题描述】:

我已经确定我需要将 Windows FILETIME 类型转换为 PHP 可以使用的类型。我希望这是一个 PHP 函数。

假设我有这个值: 60 F3 47 D1 57 98 C9 01

第1步:(感谢此页面http://www.cyanwerks.com/file-format-url.html) 我知道我需要将订单更改为 01 C9 98 57 D1 47 F3 60

第 2 步:?????

【问题讨论】:

  • 对不起,无法抗拒... 第三步:盈利!

标签: c# php datetime


【解决方案1】:

来自MSDN documentation

包含一个 64 位值,表示自 1601 年 1 月 1 日 (UTC) 以来的 100 纳秒间隔数。

【讨论】:

    【解决方案2】:

    PHP 需要什么样的时间?是time_t吗?如果是这样,只需从 unix 纪元(1970-jan-01 UTC)中减去您的 DateTime 并获取 TotalSeconds 值。

    【讨论】:

      【解决方案3】:

      感谢 Hugh Bothwell 回答这个问题: Help me translate long value, expressed in hex, back in to a date/time

      <?php
      
          // strip non-hex characters
          function hexstring($str) {
              $hex = array(
                      '0'=>'0',       '1'=>'1',       '2'=>'2',       '3'=>'3',       '4'=>'4',
                      '5'=>'5',       '6'=>'6',       '7'=>'7',       '8'=>'8',       '9'=>'9',
                      'a'=>'a',       'b'=>'b',       'c'=>'c',       'd'=>'d',       'e'=>'e',       'f'=>'f',
                      'A'=>'a',       'B'=>'b',       'C'=>'c',       'D'=>'d',       'E'=>'e',       'F'=>'f'
              );
      
              $t = '';
              $len = strlen($str);
              for ($i=0; $i<$len; ++$i) {
                      $ch = $str[$i];
                      if (isset($hex[$ch]))
                              $t .= $hex[$ch];
              }
      
              return $t;
          }
      
          // swap little-endian to big-endian
          function flip_endian($str) {
              // make sure #digits is even
              if ( strlen($str) & 1 )
                      $str = '0' . $str;
      
              $t = '';
              for ($i = strlen($str)-2; $i >= 0; $i-=2)
                      $t .= substr($str, $i, 2);
      
              return $t;
          }
      
          // convert hex string to BC-int
          function hex_to_bcint($str) {
              $hex = array(
                      '0'=>'0',       '1'=>'1',       '2'=>'2',       '3'=>'3',       '4'=>'4',
                      '5'=>'5',       '6'=>'6',       '7'=>'7',       '8'=>'8',       '9'=>'9',
                      'a'=>'10',      'b'=>'11',      'c'=>'12',      'd'=>'13',      'e'=>'14',      'f'=>'15',
                      'A'=>'10',      'B'=>'11',      'C'=>'12',      'D'=>'13',      'E'=>'14',      'F'=>'15'
              );
      
              $bci = '0';
              $len = strlen($str);
              for ($i=0; $i<$len; ++$i) {
                      $bci = bcmul($bci, '16');
      
                      $ch = $str[$i];
                      if (isset($hex[$ch]))
                              $bci = bcadd($bci, $hex[$ch]);
              }
      
              return $bci;
          }
      
          // WARNING! range clipping
          //   Windows date time has range from 29000 BC to 29000 AD
          //   Unix time only has range from 1901 AD to 2038 AD
          // WARNING! loss of accuracy
          //   Windows date time has accuracy to 0.0000001s
          //   Unix time only has accuracy to 1.0s
          function win64_to_unix($bci) {
              // Unix epoch as a Windows file date-time value
              $magicnum = '116444735995904000';
      
              $t = bcsub($bci, $magicnum);    // Cast to Unix epoch
              $t = bcdiv($t, '10000000', 0);  // Convert from ticks to seconds
      
              return $t;
          }
      
      // get input
      $dtval = isset($_GET["dt"]) ? strval($_GET["dt"]) : "0";
      $dtval = hexstring($dtval);             // strip non-hex chars
      
      // convert to quadword
      $dtval = substr($dtval, 0, 16);         // clip overlength string
      $dtval = str_pad($dtval, 16, '0');  // pad underlength string
      $quad = flip_endian($dtval);
      
      // convert to int
      $win64_datetime = hex_to_bcint($quad);
      
      // convert to Unix timestamp value
      $unix_datetime = win64_to_unix($win64_datetime);
      
      ?><html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Windows datetime test code</title>
      </head>
      
          <form method="get">
              <label>Datetime value: <input name="dt" type="text" value="<?php echo $dtval; ?>"/></label>
              <input type="submit" />
          </form>
          <hr />
          Result:
              Quad: <?php echo $quad; ?><br />
              Int: <?php echo $win64_datetime; ?><br />
              Unix timestamp: <?php echo $unix_datetime; ?><br />
              Date: <?php echo date("D, d F Y H:i:s e", $unix_datetime); ?><br />
      <body>
      </body>
      </html>
      

      【讨论】:

      • 您不可能在发布问题后的三分钟内编写完所有代码,因此除非您在提问之前知道答案,否则这不是您的代码。能否请注明出处?
      • 这是来自stackoverflow.com/questions/610603/… 的解决方案,我不能将这个问题删除为多余的。编辑回复以在到期时给予信用。
      • 处理这么简单的问题,那是一段非常难看的代码。
      猜你喜欢
      • 2014-10-03
      • 2017-01-21
      • 2015-03-13
      • 2011-04-04
      • 2011-08-30
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多