gpt4 book ai didi

PHP 考虑使用\DateTime 进行 dst 安全日期时间操作

转载 作者:可可西里 更新时间:2023-11-01 08:03:06 25 4
gpt4 key购买 nike

我有一个 php 脚本,它:

  1. 从数据库中获取旧的时间戳
  2. 使用time()函数获取当前时间戳
  3. 通过 $curr - $got 获取它们之间的差异

现在我需要将秒数转换为分钟/小时/天。我已经做了几分钟和几个小时了。但是当我尝试将差值除以 86400 时,IDE 显示错误:

PHP Consider using \DateTime for dst safe datetime manipulation

我该如何解决?

附言这是一个 PHP 脚本:

$curr = time();
$got = '1503079200'; //*Think, that this value is returned from* database row
$difference = $curr - $got;
$hrdifference= '';
$type = '';

if ( $difference >= 86400 ) {
$type = 'day';
$hrdifference = $difference/86400; //**Here it throws error**
} elseif ( $difference >= 3600 ) {
$type = 'hour';
$hrdifference = $difference/3600;
} elseif ( $difference >= 60 ) {
$type = 'minutes';
$hrdifference = $difference/60;
} elseif ( $difference < 60 ) {
$type = 'seconds';
$hrdifference = $difference;
}

echo $hrdifference . ' ' . $type;

最佳答案

我不会做这么多我会用下面的方式来做:-

<?php
// $curr = time(); // not needed
$got = '1503079200'; //value is returned from database row

$datetime1 = new DateTime(); // current date-time
$datetime2 = new DateTime(date('Y-m-d',$got)); // get date-time based on $got
$interval = $datetime1->diff($datetime2); // get difference
echo $interval->format('%d')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes"; //print days hour minute

输出:- https://eval.in/850420

关于PHP 考虑使用\DateTime 进行 dst 安全日期时间操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com