如何在php中从两个不同的日期和时间计算价格

时间:2018-02-12 11:48:48

标签: php price

我想从两个不同的日期和时间计算价格,即

$price = 20;
$tripStart = "2018-02-09";
$tripEnd = "2018-02-12";
$tripStartTime = "09:00:00";
$tripStartEnd = "19:00:00";

我想计算日期,但我如何计算日期和时间。

目前我正在计算日期。

$date1 = new DateTime($tripStart);
$date2 = new DateTime(  $tripEnd  );
$days = $date2->diff($date1)->format("%a");

它工作正常,但我想用时间计算日期。

2 个答案:

答案 0 :(得分:1)

您可以计算确切的时间戳并执行此操作

$start_dt = strtotime($tripStart + " " + $tripStartTime);
$end_dt = strtotime($tripEnd + " " + $tripStartEnd);

# 86400 is number of seconds in days
$exact_duration_in_days = ($end_dt - $start_dt)/86400;

答案 1 :(得分:1)

您可以简单地连接开始日期时间和结束时间并执行此操作:

    <?php
$tripStart = "2018-02-09";
$tripEnd = "2018-02-12";
$tripStartTime = "09:00:00";
$tripStartEnd = "19:00:00";

$start_date = new DateTime($tripStart ." ".$tripStartTime );
$end_date = new DateTime($tripEnd ." ".$tripStartEnd );
$interval = $start_date->diff($end_date);
print_r($interval);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
echo $interval->h." hours ";
?>