0

I am trying to get the hours between two dates but it doesnt seem to give me the right results.

global $wpdb;
global $wp;
$wp->query_vars['sp_date'] = "10/06/2014";
$wp->query_vars['sp_time_period'] = "2pm to 3pm";
// only process requests with "my-plugin=ajax-handler"
    date_default_timezone_set('America/New_York');
    $date = false;
    $time_span = false;
    $dayoffs = false;
    if(isset($wp->query_vars['sp_date'])) {
        $date = $wp->query_vars['sp_date'];
    }
    if(isset($wp->query_vars['sp_time_period'])) {
        $period = $wp->query_vars['sp_time_period'];
    }
    if(isset($wp->query_vars['get_dayoffs'])) {
        $dayoffs = $wp->query_vars['get_dayoffs'];
    }
    $returnValue = 0;
    if($date || $period) {
        $hrs = 0;
        if($date && !$returnValue) {
            $_SESSION['sp_date'] = $date;
            $returnValue = 1;
        }
        if($period && !$returnValue) {
            $_SESSION['sp_time_period'] = $period;
            $returnValue = 1;
        }
        if($_SESSION['sp_date']) {
            $hrsTilNoonBool = 0;
            $nToday = date("Y-m-d h:i:s");
            $deliveryDates = $_SESSION['sp_date']." 18:00:00";
            $deliveryDateNoon = $_SESSION['sp_date']." 12:00:00";

            $date1 = new DateTime($nToday);
            $date2 = new DateTime($deliveryDates);
            $date3 = new DateTime($deliveryDateNoon);
            $diff = $date2->diff($date1);
            $diffNoon = $date3->diff($date1);
            $hrs = $diff->format('%H');
            $hrsTilNoon = $diffNoon->format('%h');
            if($hrsTilNoon) {
                $hrsTilNoonBool = 1;
            }
        }
        $settings = $wpdb->get_results("SELECT * FROM sp_settings WHERE id = 1");
        $content = '{"value":'.$returnValue.', "over_twentyfour":'.$hrs;
        if(!$settings[0]->payment_id_before)
            $settings[0]->payment_id_before = 53;
        if(!$settings[0]->payment_id_after)
            $settings[0]->payment_id_after = 54;
        $content .= ',"payment_before":'.$settings[0]->payment_id_before.',"payment_after":'.$settings[0]->payment_id_after.',"before_afternoon":'.
            $hrsTilNoonBool.'}';
    }
    if($dayoffs) {
        $dates = $wpdb->get_results("SELECT * FROM sp_date_blacklist");
        $datesArry  = array();
        foreach($dates as $d) {
            $datesArry[] = $d->date;
        }
        $content = json_encode($datesArry);
    }
    echo $content;
    exit();

I first put in a target date $wp->query_vars['sp_date'] = "10/06/2014"; Then I take todays date which is 10/03/2014.

        $diff = $date2->diff($date1);
        $diffNoon = $date3->diff($date1);
        $hrs = $diff->format('%H');

The days say 3 which is right. but the hours come out to be 14 hours ? I am confused. Perhaps maybe I am not reading something correctly

enter image description here

2 Answers 2

1

Try appending the same time to each timestamp, e.g. ' 00:00:00'. When you take today's date, it takes the current time too. The created date will be midnight by default.

Sign up to request clarification or add additional context in comments.

Comments

1

You're feeing in ambiguous dates:

$d1 = '01/02/2013';
$d2 = '01-02-2013';

$ts1 = strtotime($d1); // 1357106400
$ts2 = strtotime($d2); // 1359698400

echo date('r', $ts1); // Wed, 02 Jan 2013 00:00:00 -0600
echo date('r', $ts2); // Fri, 01 Feb 2013 00:00:00 -0600

the / v.s. - separator in strtotime() DOES make a major difference. It's european dd-mm-yyyy v.s. american mm/dd/yyyy

You should probably use date_create_from_format() and EXCPLICITLY specify the format of your input date strings.

1 Comment

I dont think it makes a difference rather is european or not. even if I change the values to match format, it still gives me the incorrect informtation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.