了解PHP日期差异

时间:2014-10-03 19:17:50

标签: php mysql wordpress

我试图获得两个日期之间的时间,但它似乎没有给我正确的结果。

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();

我首先输入目标日期$wp->query_vars['sp_date'] = "10/06/2014";然后我今天的日期是10/03/2014。

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

日子说3是对的。但是时间是14小时?我很迷惑。也许我可能没有正确阅读

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试将相同的时间附加到每个时间戳,例如'00:00:00'。当你今天的日期,它也需要当前的时间。默认情况下,创建日期为午夜。

答案 1 :(得分:1)

你在模棱两可的日子里收费:

$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

/ v。 strtotime中的-分隔符()会产生重大影响。这是欧洲的dd-mm-yyyy v.s.美国mm / dd / yyyy

您应该使用date_create_from_format()和EXCPLICITLY指定输入日期字符串的格式。