开发者

PHP Dates: Syntax Debug on PHP if conditional

开发者 https://www.devze.com 2023-02-11 23:37 出处:网络
In the following code $start is a start date manually entered into a datepicker and $end is a separate key also, entered via datepicker.These are being compared against date(\'ymd\'), which is today.

In the following code $start is a start date manually entered into a datepicker and $end is a separate key also, entered via datepicker. These are being compared against date('ymd'), which is today.

Early in the code for this plugin we have this code (where the same argument returns true):

//Parse End Date
if($end):
    $end = explode('-', $end);
    $end = mkt开发者_运维知识库ime($hour, $_POST['_date_minute'], 0, $end[0], $end[1], $end[2]);
        if ((date('ymd',$start) < date('ymd',$end)) && (date('ymd',$end) >= date('ymd'))) {
            $compare = date('ymd'); //Overwrite start date $compare
        }
        else {
            $compare = date('ymd', $start);         
        }
endif;

Later in the code, the same argument returns false here:

function event_list_date($start_or_end, $format, $echo = true){
    global $post;

    // Check the end date, if it's greater than today and then start date is less than or equal to today, round it off so that it's today and doesn't look like the event is already past

    // Original Code
    //  if ((date('ymd',$start) < date('ymd',$end)) && (date('ymd',$end) >= time('ymd'))) {

    // Stackoverflow Proposed Change
    //  if ($start < $end && $end >= time('ymd')) {

    if ($start < $end && $end >= time('ymd')) {
        $start = date('ymd'); //Overwrite start date $compare
    }
    else {
        $start = get_post_meta($post->ID, '_date_start', true);;            
    }

    $end = get_post_meta($post->ID, '_date_end', true);

    if($start_or_end == 'start'):
        $date = date($format, $start);
        if($echo): echo $date; else: return $date; endif;
    elseif($start_or_end == 'end'):
        $date = date($format, $end);
        if($echo): echo $date; else: return $date; endif;
    endif;   
}

Can someone tell me why the if statement is returning false for an event with a $start value equal to yesterday and an $end equal to 5 days from now?

EDIT: Posting a "zoomed out" context of this code


The date function changes these into strings. Then you're using mathematical comparison operators to compare the strings, which does not work in the way you're wanting. If you want to compare a date against another date, you'll want to convert them to something more easily compared, like numbers.

strtotime will take care of that! Since you're passing $start into date() (which requires a numeric argument), I assume $start is already a numeric time representation. So, to compare against right now, use time();

strtotime docs

date docs

0

精彩评论

暂无评论...
验证码 换一张
取 消