douchao9899 2010-02-19 20:28
浏览 235
已采纳

如何将COledatetime转换为unix时间/ php时间?

How can I convert a COleDateTime::GetCurrentTime() to a unix time stamp or more specifically I want to convert the value so I can use it in PHP with the date() function?

  • 写回答

3条回答 默认 最新

  • dtjw6660 2010-02-22 07:53
    关注

    That sounds like the DATE type:

    The DATE type is implemented using an 8-byte floating-point number. Days are represented by whole number increments starting with 30 December 1899, midnight as time zero. Hour values are expressed as the absolute value of the fractional part of the number.

    test code:

    COleDateTime cdt(2010, 2, 15, 0, 0, 0); // 2010-02-15 12:00 AM
    DATE d = cdt; // using COleDateTime::operator DATE  
    printf("%f", d);
    

    prints 40224.000000.

    I don't think there is a built-in function to convert this back to any "native" php date/time value (could be wrong though).

    edit: A (probably naive) starting point for a conversion function...
    some sample data create by

    LPCTSTR data[] = {
      _T("1976-03-27 05:54:00"),
      _T("1984-04-01 11:55:55"),
      _T("1996-01-25 08:30:00"),
      _T("2000-01-01 08:30:00"),
      _T("2010-02-14 00:00:00"),
      _T("2010-02-14 23:59:59"),
      _T("2010-02-15 00:00:00"),
      _T("2010-02-15 00:00:01"),
      _T("2010-02-23 15:30:00"),
      NULL
    };
    COleDateTime cdt;
    for(int i=0; data[i]; i++) {
      if ( !cdt.ParseDateTime(data[i]) ) {
        _tprintf(_T("%s: error
    "), data[i]);
      }
      else {
        _tprintf(_T("'%s'=>'%f'
    "), data[i], (DATE)cdt);
      }
    }
    

    lead to

    function DATE2unix($d) {
      static $jd2DATE_offset = 2415019;
      $date = intval($d); 
      $time = fmod($d, 1); 
      $ts = jdtounix($date+$jd2DATE_offset) + round($time*86400);
      return $ts;
    }
    
    $data = array(
      '1976-03-27 05:54:00'=>'27846.245833',
      '1984-04-01 11:55:55'=>'30773.497164',
      '1996-01-25 08:30:00'=>'35089.354167',
      '2000-01-01 08:30:00'=>'36526.354167',
      '2010-02-14 00:00:00'=>'40223.000000',
      '2010-02-14 23:59:59'=>'40223.999988',
      '2010-02-15 00:00:00'=>'40224.000000',
      '2010-02-15 00:00:01'=>'40224.000012',
      '2010-02-23 15:30:00'=>'40232.645833'
    );
    foreach($data as $target=>$d ) {
      $ts = DATE2unix($d);
      $date = gmdate('Y-m-d H:i:s', $ts);
      echo 'd=', $d, ' | target=', $target, ' | date=', $date, ' : ', ($target===$date) ? 'ok':'error', "
    ";
    }
    

    which prints

    d=27846.245833 | target=1976-03-27 05:54:00 | date=1976-03-27 05:54:00 : ok
    d=30773.497164 | target=1984-04-01 11:55:55 | date=1984-04-01 11:55:55 : ok
    d=35089.354167 | target=1996-01-25 08:30:00 | date=1996-01-25 08:30:00 : ok
    d=36526.354167 | target=2000-01-01 08:30:00 | date=2000-01-01 08:30:00 : ok
    d=40223.000000 | target=2010-02-14 00:00:00 | date=2010-02-14 00:00:00 : ok
    d=40223.999988 | target=2010-02-14 23:59:59 | date=2010-02-14 23:59:59 : ok
    d=40224.000000 | target=2010-02-15 00:00:00 | date=2010-02-15 00:00:00 : ok
    d=40224.000012 | target=2010-02-15 00:00:01 | date=2010-02-15 00:00:01 : ok
    d=40232.645833 | target=2010-02-23 15:30:00 | date=2010-02-23 15:30:00 : ok
    

    Though these 9 samples seem to work ok I'm not sure about intval()/round() and whether there are other conventions the php code must follow. If you're going to use this function, test it And then test it again with a lot more samples.
    And keep in mind that this is limited to the unix timestamp range which is smaller than the range of COleDateTime.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能