skip to Main Content

Maybe you can help me.
I need to improve this functionality to check the date in ACF with the real date and output different function values. When event date end return "END" or "0". Else return "cont" or "1"

Unfortunately, it absolutely does not want to check the dates)

function add_calendar() {  
$cur_date = date("YmdTHisZ", time());
$starttime =  date('YmdTHisZ', strtotime(get_field('start')));  
$endtime = date('YmdTHisZ', strtotime(get_field('end')));  
$title = get_the_title();
if($cur_date <= $endtime){
$string = "https://calendar.google.com/calendar/render?action=TEMPLATE&dates=" . $starttime . "%2F" . $endtime . "&details=&location=&text=" . $title;
return $string;
} else {
    return "#";
}
}

2

Answers


  1. Chosen as BEST ANSWER

    Solution

    function add_calendar() {
    $cur_date = date("Y-m-d", time());
    $starttime =  date('YmdTHisZ', strtotime(get_field('start')));
    $endtime = date('YmdTHisZ', strtotime(get_field('end')));
    $close_event = date('Y-m-d', strtotime(get_field('end')));
    $title = get_the_title();
    if($cur_date <= $close_event){
    $string = "https://calendar.google.com/calendar/render?action=TEMPLATE&dates=" . $starttime . "%2F" . $endtime . "&details=&location=&text=" . $title;
    return $string;
    } else {
        return "#";
    }
    } 
    

  2. I haven’t tested this but you might try using PHP’s DateTime() class. It’s more reliable and I believe it supports date comparisons in the way you’re using:

    function add_calendar() {  
        $cur_date = new DateTime();
        $starttime = new DateTime(get_field( 'start' ));  
        $endtime = new DateTime(get_field( 'end' ));  
        $title = get_the_title();
        if ( $cur_date <= $endtime ) {
            $string = 'https://calendar.google.com/calendar/render?action=TEMPLATE&dates=' . $starttime->format( 'YmdTHisZ' ) . '%2F' . $endtime->format( 'YmdTHisZ' ) . '&details=&location=&text=' . $title;
            return $string;
        } else {
            return '#';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search