PHP - 如果时间在晚上 8 点到早上 8 点之间,则增加额外的运费

问题描述

我有一个食品订购脚本,我想添加如果交货时间在晚上 8:00:00 到早上 7:59:59 之间,则要添加 5 美元的额外费用,如果在上午 8:00:00 到早上 7 点之间:59:59 PM 保持原样交付。

这是我的代码,用于计算运费,返回我们已经在仪表板中设置的运费。

我们希望将此添加为一项修复,以增加我们夜间送货的费用。

当日派送 = 正常派送费 夜间派送 = 正常派送费 + 5

谢谢,希望有人能指导我。

    public static function verifyLocation($merchant_id=0,$lat=0,$lng=0,$order_subtotal=0)
{
    $resp = Yii::app()->db->createCommand()
      ->select('merchant_id,latitude,lontitude,minimum_order')
      ->from('{{merchant}}')        
      ->where("merchant_id=:merchant_id",array(
         ':merchant_id'=>(integer)$merchant_id,)) 
      ->limit(1)   
      ->queryRow(); 
    if($resp){
        
        $provider = FunctionsV3::getMapProvider();          
        MapsWrapper::init($provider);
        $unit = FunctionsV3::getMerchantdistanceType($merchant_id); 
        $mode = isset($provider['mode'])?$provider['mode']:'driving';
            
                    
        $merchant_delivery_distance = getoption($merchant_id,'merchant_delivery_miles');   
        
        /*GET DELIVERY FEE*/
        $delivery_fee = getoption($merchant_id,'merchant_delivery_charges');    
        $resp_distance = array();
                
        if($merchant_delivery_distance>0){
            $resp_distance = MapsWrapper::getdistance($resp['latitude'],$resp['lontitude'],$lat,$lng,$unit,$mode);
            
            $distance = $resp_distance['distance'];
            if($merchant_delivery_distance>0){
                if($distance>$merchant_delivery_distance){                  
                   $pretty_distance = Yii::t("default","[distance] [unit]",array(
                                  '[distance]'=>$merchant_delivery_distance,'[unit]'=>MapsWrapper::prettyUnit($unit)
                                ));
                    $error = Yii::t("default","Sorry but this merchant delivers only with in [distance] your current distance is [current_distance]",array(
                       '[distance]'=>$pretty_distance,'[current_distance]'=>$resp_distance['pretty_distance']
                    ));
                    throw new Exception( $error );
                } 
            }
            
            /*MINIMUM ORDER TABLE*/
            $min_tables_enabled = getoption($merchant_id,'min_tables_enabled');
            if($min_tables_enabled==1){                                     
                $min_order = self::getMinimumOrderTable(
                   $merchant_id,$resp_distance['distance'],$resp_distance['unit'],$resp['minimum_order']
                );                  
                if($min_order>$order_subtotal){
                    $error = Yii::t("default","Sorry but minimum order is [min_order] for distance [distance]",array(
                     '[min_order]'=>FunctionsV3::prettyPrice($min_order),'[distance]'=>$resp_distance['pretty_distance']
                   ));
                   throw new Exception( $error );
                }
            }
            
            /*SHIPPING FEE*/
            $shipping_enabled = getoption($merchant_id,'shipping_enabled');
            if($shipping_enabled==2){                   
                $delivery_fee = self::getShippingFee($merchant_id,$delivery_fee);
            }       
        } 
                    
        return array_merge((array)$resp_distance,array('delivery_fee'=>$delivery_fee));
        
    } else throw new Exception( t("Merchant not found") );
}   

解决方法

您可以通过使用 php date 获取所需时间之间的时间来实现这一点,这样您就可以相应地增加费用 *注意:PHP从服务器获取日期,如果您要根据个人用户时区设置价格。应使用Javascript与PHP集成

<?php 
date_default_timezone_set('Africa/Johannesburg');   //Set your timezone here get php timezone from 

https://www.php.net/manual/en/timezones.php


$normalfee = "500"; //The variable containing the normal fee 
$tax = "5"; //The added tax for after 8pm fee increase

$getdate = date("Hi"); //Get some time from the server

echo $getdate."Debug : The current time now<br>"; //For debug purposes

if(($getdate > "759") && ($getdate < "2000")){

$finalfee = $normalfee; 

echo $finalfee."Debug : Still the same fee<br>"; //The fee should remain normal starting at 8:00am till 7:59:59pm


} else { 

$finalfee = $normalfee + $tax; 

echo $finalfee."Night time fee added<br>"; //The fee will increase by 5 starting from 8:00pm till 7:59:59am

 }


echo "Debug variable passed out of condition the dollar sign can now be added $".$finalfee."<br>"; 
//Again Debugging this is to show you can then use the $finalfee variable outside the conditions


?>

脚本已清理...

<?php 
date_default_timezone_set('Africa/Johannesburg');

$normalfee = "500"; 
$tax = "5";
$getdate = date("Hi");

if(($getdate > "759") && ($getdate < "2000")){

$finalfee = $normalfee; 

} else { 

$finalfee = $normalfee + $tax; 

 }

$display = "Final price $".$finalfee;

?>