php - Total days error -
when start date ex:6/1/2016 , end date ex : 8/1/2016
it process computational days, process result of 3 days 6/1/2016 day 7/1/2016 day 8/1/2016 day
this wrong .
must 6/1/2016 7/1/2016 day , 7/1/2016 8/1/2016 day
so sum 2 day not 3 days
$start_date = strtotime($date_from); $end_date = strtotime($date_to); $datetime1 = date_create($date_from); $datetime2 = date_create($date_to); $interval = date_diff($datetime1, $datetime2); $cs_booking_days = $interval->days; // loop between timestamps, 24 hours @ time $total_price = ''; $adult_price = 0; $pricings = get_option('cs_price_options'); $cs_offers_options = get_option("cs_offers_options"); $pricings_array = $pricings[$post_id]; if (isset($pricings[$post_id]['cs_plan_days'])) { $cs_sp_days = $pricings[$post_id]['cs_plan_days']; } $pricing_data = array(); $brk_counter = 0; $total_orignal = 0; $price['total_price'] = 0; $flag = false; ($i = $start_date; $i <= $end_date; $i = $i + 86400) { $total_days++; $brk_counter++; $thisdate = date('y-m-d', $i); // 2010-05-01, 2010-05-02, etc $day = strtolower(date('d', strtotime($thisdate))); $adult_price = $pricings_array['cs_pricing_branches']['adult_' . $day . '_price'][0]; $adult_temp_price = $adult_price != '' ? $adult_price : 0; $adult_price = $adult_temp_price; $to_check_date = strtotime(date('y-m-d', $i));
<?php // code goes here $starttimestamp = strtotime("2016/01/06"); $endtimestamp = strtotime("2016/01/08"); $timediff = abs($endtimestamp - $starttimestamp); $numberdays = $timediff/86400; // 86400 seconds in 1 day // , might want convert integer $numberdays = intval($numberdays); echo $numberdays;
back code => replace
for ($i = $start_date; $i <= $end_date; $i = $i + 86400) {
this
for ($i = $start_date; $i < $end_date; $i = $i + 86400) {
<= problem, type <
explanation
06.01 - 08.01
first loop: $i = 06.01 , <= 08.01
second loop: $i = 07.01 , <= 08.01
third loop: $i = 08.01 , <= 08.01 - still true, sum = 3 days
after change
first loop: $i = 06.01 , < 08.01
second loop: $i = 07.01 , < 08.01
third loop: $i = 08.01 , < 08.01 - false, sum = 2 days
Comments
Post a Comment