dtmooir3395 2014-09-04 13:07 采纳率: 0%
浏览 95
已采纳

18位Julian Date / TimeStamp PHP转换?

I am currently working on a project for my work, it is using a lot of data from the mysql databases, alot of rows from the databases have a TimeStamp but they are 18 digits long.

I have a calculation that converts it into a friendly date in excel, DD-MM-YY HH:MM:SS.

The calculation is as follows (212262066420979000/86400000000) - 2415018.5 = value, and then format in excel into dd-mm-yy hh-mm-ss.

If I use php to do this calculation, I get the right value, but I can't set the format to look like a friendly date like you can in excel.

Is there any easy way of doing this in php?

Here is my code below

$sql = ("SELECT * FROM varmeas WHERE VARIABLE = 'WTSE';");

$result = mysqli_query($conn, $sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysqli_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

echo "<table border='1'>
<tr>
<th>ROUTING_POS</th>
<th>VARIABLE</th>
<th>VAR_MEASURE_1</th>
<th>DATETIME_STAMP</th>
<th>FRIENDLY_DATE</th>
</tr>";

while ($row = mysqli_fetch_assoc($result)) {

  echo "<tr>";
  echo "<td>" . $row['ROUTING_POS'] . "</td>";
  echo "<td>" . $row['VARIABLE'] . "</td>";
  echo "<td>" . $row['VAR_MEASURE_1'] . "</td>";
  echo "<td>" . $row['DATETIME_STAMP'] . "</td>";
  echo "</tr>";


}
echo "</table>";

?>

This would output a table like -

ROUTING_POS VARIABLE    VAR_MEASURE_1          DATETIME_STAMP   
FLIS        WTSE            22.20             212262066888394000    
  • 写回答

1条回答 默认 最新

  • doucuoyan0426 2014-09-04 13:30
    关注
    (212262066420979000/86400000000) - 2415018.5 = value
    

    Gives a value of 41718.379872442, which is an MS Excel timestamp value for 20/03/2014 09:07:01

    To convert from an Excel timestamp to a Unix timestamp (based on the Windows 1900 calendar)

    For a quick and dirty conversion from an Excel timestamp to a unix timestamp that can be used with PHP date functions:

    $excelValue =  (212262066420979000/86400000000) - 2415018.5;
    $unixValue = ($excelValue - 25569) * 86400;
    

    Note that this will return a value in UST

    date_default_timezone_set('UTC');
    echo date('Y-m-d H:i:s', $unixValue);
    

    or

    $d = new DateTime('@' . (int)$unixValue, new DateTimeZone('UTC'));
    echo $d->format('Y-m-d H:i:s');
    

    EDIT

    To integrate it with your existing code is as simple as adding:

    $excelValue =  ($row['DATETIME_STAMP'] / 86400000000) - 2415018.5;
    $unixValue = ($excelValue - 25569) * 86400;
    $d = new DateTime('@' . (int)$unixValue, new DateTimeZone('UTC'));
    echo "<td>" . $d->format('Y-m-d H:i:s') . "</td>";
    

    immediately after

    echo "<td>" . $row['DATETIME_STAMP'] . "</td>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?