index.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. use common\helpers\Html;
  3. $this->title = '时间戳转换';
  4. $this->params['breadcrumbs'][] = ['label' => $this->title];
  5. ?>
  6. <div class="row">
  7. <div class="col-lg-12">
  8. <div class="box">
  9. <div class="box-header with-border">
  10. <h3 class="box-title"><?= Html::encode($this->title) ?></h3>
  11. </div>
  12. <div class="box-body">
  13. <div class="col-sm-6">
  14. <div class="form-group field-menu-title required">
  15. <span>现在时间</span>:<span id="time"></span><br>
  16. <span>现在时间戳</span>:<span id="timestamp"></span>
  17. </div>
  18. <div class="input-group m-b">
  19. <input type="text" class="form-control t1" name="timestamp" placeholder="时间戳">
  20. <span class="input-group-btn" title="立即点击"><button class="btn btn-white transform1">时间戳转换日期</button></span>
  21. <input type="text" class="form-control d1" name="datetime" placeholder="日期" readonly>
  22. </div>
  23. <div class="input-group m-b">
  24. <input type="text" class="form-control d2" name="timestamp" placeholder="日期">
  25. <span class="input-group-btn"><button class="btn btn-white transform2">日期转换时间戳</button></span>
  26. <input type="text" class="form-control t2" name="datetime" placeholder="时间戳" readonly>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. </div>
  33. <script>
  34. $(document).ready(function(){
  35. var date = new Date();
  36. var timestamp = parseInt(date.getTime() / 1000);
  37. $('.t1').val(timestamp);
  38. $('.d1').val(timestampToTime(timestamp));
  39. $('.d2').val(timestampToTime(timestamp));
  40. $('.t2').val(timestamp);
  41. setTime();
  42. setInterval(setTime, 1000);
  43. });
  44. $('.transform1').click(function () {
  45. var val = $('.t1').val();
  46. if (val.length === 0) {
  47. rfMsg('请填写内容');
  48. return;
  49. }
  50. $('.d1').val(timestampToTime(val));
  51. });
  52. $('.transform2').click(function () {
  53. var val = $('.d2').val();
  54. if (val.length === 0) {
  55. rfMsg('请填写内容');
  56. return;
  57. }
  58. $('.t2').val(timeToTimestamp(val));
  59. });
  60. function setTime() {
  61. $("#timestamp").text(timeToTimestamp());
  62. $("#time").text(timestampToTime(timeToTimestamp()));
  63. }
  64. function timestampToTime(timestamp) {
  65. var date = new Date(timestamp * 1000), str = '';//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  66. str += date.getFullYear() + '-'; // 获取当前年份
  67. str += (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  68. str += (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  69. str += (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  70. str += (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
  71. str += (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
  72. return str;
  73. }
  74. function timeToTimestamp(time = '') {
  75. var date;
  76. if (time.length > 0) {
  77. date = new Date(time);
  78. } else {
  79. date = new Date();
  80. }
  81. return parseInt(date.getTime() / 1000);
  82. }
  83. </script>
粤ICP备19079148号