2017-03-04 62 views
-1

$ifvalid如果是真的應該返回1. 在以下情況下whenvalue是真實的,它返回10 如果我把它退出正常返回1 爲什麼會這樣?返回10,而且沒有退出-PHP

Ajax調用

JS -Calling AJAX

var data = { 
    action: "validate_date", 
    start_date: 22/2/2017, 
    end_date: 22/7/2017, 
}; 

$.post(ajaxurl, data, function (response) { 
    alert(response) 
}); 

PHP

<?php 
    function validate_date(){ 
     $start_date = $_POST['start_date']; 
     $expiration_date= $_POST['end_date']; 
     $ifValid = check_in_range($start_date, $expiration_date); 

     echo $ifValid; //returns 10 
     echo $ifValid;exit; //returns 1 
    } 

    function check_in_range($start_date, $end_date) 
    { 
     // Convert to timestamp 
     $start_ts = strtotime($start_date); 
     $end_ts = strtotime($end_date); 
     $timeNow = strtotime("now"); 
     // Check that user date is between start & end 
     return (($timeNow >= $start_ts) && ($timeNow <= $end_ts)); 
    } 
?> 

在這個AJAX的迴應,我做alert(response);

+0

共享功能'check_in_range'? – C2486

+0

@Niklesh完成.. –

+0

downvote的原因是什麼? –

回答

0

您可以設置功能類似下面

function check_in_range($start_date, $end_date) 
{ 
    // Convert to timestamp 
    $start_ts = strtotime($start_date); 
    $end_ts = strtotime($end_date); 
    $timeNow = strtotime("now"); 

    // Check that user date is between start & end 
    if(($timeNow >= $start_ts) && ($timeNow <= $end_ts)){ 
     return '1'; // OR return '0'; as you want 
    }else{ 
     return '0'; // OR return '1'; as you want 
    } 
}