PHP - Adding extra shipping fee if time is between 8pm and 8am

116 Views Asked by At

I have a food ordering script which I want to add If delivery time is between 8:00:00PM - 7:59:59AM to add an extra fee of $5 and if it is between 8:00:00 AM and 7:59:59 PM to keep delivery as is in the script.

This is my code that calculates the delivery fee that returns the delivery fee we already have set in our dashboard.

We want to add this as a fix to add a fee for our night deliveries.

Day deliveries = Normal delivery Fee Night Delivery = Normal delivery fee + 5

Thank you and I hope someone can guide me.

    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,$resp_distance['distance'],$resp_distance['unit'],$delivery_fee);
            }       
        } 
                    
        return array_merge((array)$resp_distance, array('delivery_fee'=>$delivery_fee));
        
    } else throw new Exception( t("Merchant not found") );
}   
1

There are 1 best solutions below

3
On BEST ANSWER

You could achieve that by using php date to get time between desired time so you can increase the fee accordingly *Note : PHP gets date from server ,if you were to set the price based on the individual user timezone.Javascript should be used to integrate with 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


?>

The script cleaned up...

<?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;

?>