为什么我无法从数据库检索日期?

huangapple go评论79阅读模式
英文:

Why i cant retrieve the date from database

问题

I want to calculate the days between two dates and the dates are being retrieved from the database. Below is what I have done:

if($model->save()){
    $date1 = strtotime($startDate);
    $date2 = strtotime($endDate);

    $days = ($date2 - $date1);
    $Days = ($days/(60*60*24));
    $model->no_of_days = $Days;
    $model->save();
}

$startDate and $endDate are being retrieved from the database. When I code it like this, the result is 0.

But when I replace it with a date, for example, '2020-10-10' and '2020-10-13', the result is 3.

My format for the date from the database is the same as the replaced date. Can someone guide me and explain to me what is wrong with my code?

英文:

Okay first of all, I have checked many questions on stackoverflow and those questions doesn't seems to solve my problem. Before some of you might put this question into a duplicate question, Can I have any explanation whats wrong with my code.

I want to calculate the days between two dates and the dates is being retrieved from database.
Below is what I have done

if($model->save()){
    $date1 = strtotime($startDate);
    $date2 = strtotime($endDate);
    
    $days = ($date2 - $date1);
    $Days = ($days/(60*60*24));
    $model->no_of_days= $Days;
    $model->save();
    
}

$startDate and $endDate are being retrieved from the database. When I code it like this, the result is 0.

but, when I replace it with a date, for example, '2020-10-10' and '2020-10-13', the result is 3.

My format for the date from the database is the same like the replaced date. Can someone guide me and explain to me.
What is wrong with my code?

答案1

得分: 2

步骤一: 我建议避免使用 daysDays 作为变量。

步骤二: 使用原生的 var_dump 函数验证 $startDate$endDate 的内容。

var_dump($startDate);
var_dump($endDate);

如果从数据库获取的数据是 'good',那么您应该得到正确的结果。因此,我们认为您的 $startDate$endDate 没有被正确初始化。

$date1 = strtotime('2020-10-10');
$date2 = strtotime('2020-10-13');
$days = ($date2 - $date1);
var_dump($days); // 返回 259200
$days = ($days / (60 * 60 * 24));
var_dump($days); // 返回 3

您的代码应该是这样的:

if($model->save()){
    $date1 = strtotime($startDate);
    $date2 = strtotime($endDate);

    $days = ($date2 - $date1);
    $days = ($days / (60 * 60 * 24));
    $model->no_of_days = $days;
    $model->save();
}

步骤三: 我认为您应该查看 DatetimeInterval 类或 date_diff 函数。

英文:

Step One: I suggest to avoid using daysand Daysas variables.

Step Two: Verify the content of $startDate and $endDateby dumping them with the native var_dump function.

var_dump($startDate);
var_dump($endDate);

If data from your database are 'good', you should have the correct result. So we think that your $startDate and $endDate are not well initialized.


    $date1 = strtotime('2020-10-10');
    $date2 = strtotime('2020-10-13');
    $days = ($date2 - $date1);
    var_dump($days); //returns 259200
    $days = ($days/(60*60*24)); 
    var_dump($days); //returns 3

You code should be:

if($model->save()){
        $date1 = strtotime($startDate);
        $date2 = strtotime($endDate);

        $days = ($date2 - $date1);
        $days = ($days/(60*60*24));
        $model->no_of_days= $days;
        $model->save();

}

Step3:
I think you should have a look on DatetimeInterval class or date_diff function.

答案2

得分: 1

根据您说的,如果替换日期的格式与数据库中的日期格式相同,您可以尝试使用以下方式:

注意您正在使用$days和$Days变量。可以使用一个单独的$days变量来存储日期差异的结果,并可以将该变量用于保存到数据库中。我已将$Days更改为$days。

if($model->save()){
     // 计算时间戳之间的差异
     $days = strtotime($endDate) - strtotime($startDate); 
    // 1天 = 24小时 
    // 24 * 60 * 60 = 86400秒 
    $days = abs(round($days/ 86400)); 
    $model->no_of_days = $days;
    $model->save();
}
英文:

As you said,if the format of the replaced date is same as that of the date from the database you can try using the following:
Note : you are using $days and $Days variable. Use can use a single $days variable to store the date difference result and can use that variable for saving into database. I have changed using $Days into $days.

if($model->save()){
         // Calulating the difference in timestamps 
         $days = strtotime($endDate) - strtotime($startDate); 
        // 1 day = 24 hours 
        // 24 * 60 * 60 = 86400 seconds 
        $days= abs(round($days/ 86400)); 
        $model->no_of_days= $days;
        $model->save();

    }

答案3

得分: 0

使用date_diff

$date1 = date_create($startDate);
$date2 = date_create($endDate);
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a");

如果你传递正确的$startDate$endDate,它应该正常工作。
希望这对你有帮助。

英文:

Use date_diff

  $date1=date_create($startDate);
  $date2=date_create($endDate);
  $diff=date_diff($date1,$date2);
  echo $diff->format("%R%a");

If you pass correct $startDate and $endDate it should works correct.
Hope this help to you.

huangapple
  • 本文由 发表于 2020年1月6日 16:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定