Pergunta

Some background information(you can skip it if you want) The project is as followed i have a arduino hooked up to the internet to controll some relay's for the heating at my local scouting building, when an attendece is present i want the relay to go on, this part of the project is to get a date and time from the google calendar and check if the appointment is at this point happening, so the relay can be turned on. this script will be running with a cron job so that every 5 minutes or so the conditions will be checked whether or not a relay has to be opened, problem is i cant seem to check if the time is between the start and end time. so know you know a little background information, it may help with solving or improving the code :) it is probebly something very stuppid but after hours of trying i return to you guys!

the code I have the following code :

<?php
error_reporting(E_ALL);
   ini_set('display_errors', '1');
  $url='https://www.googleapis.com/calendar/v3/calendars/ijo7cj2i5dl958l15e4tml6pk8%40group.calendar.google.com/events?maxResults=10&orderBy=startTime&showDeleted=false&singleEvents=true&fields=items(end%2Cstart%2Csummary)&key=AIzaSyCBCQC6N55-k8TpOPkVNdyuVHV2MAp5-DI';
// create a new cURL resource

function download_page($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $retValue = curl_exec($ch);          
    curl_close($ch);
    return $retValue;
}

$json=json_decode(download_page($url),true);
$i=0;

foreach($json["items"] as $item){
    $dateNOW= new DateTime('NOW');
    $dateNOW->add(new DateInterval('PT1H00S'));
    echo $item["summary"]."<br>";
    //echo $test["start"]["dateTime"]."<br>";
    $dateBegin = DateTime::createFromFormat('Y-m-d\TH:i:sP',$item["start"]["dateTime"]);
    echo $dateBegin->format('d-m-Y H:i')."<br>";
    $dateEnd = DateTime::createFromFormat('Y-m-d\TH:i:sP',$item["end"]["dateTime"]); 
    echo $dateEnd->format('d-m-Y H:i')."<br>";
    echo $dateNOW->format('d-m-Y H:i')."<br>";
    if($dateNOW>$dateBegin){
        echo "Tim/date is bigger then datebegin<br>";
        if($dateNOW<$dateEnd){
            echo"Tim/date is bigger then dateEND<br>";
        }
    }
}
?>

My problem is that ones i run the program the Tim/date is bigger then datebegin will do its work correctly but the Tim/date is bigger then dateEND wont show up at all. i tried a lot of diffrenent things from all kind of resources but i just cant get it to work, could anybody please help?

BTW error reporting is just for my server since i normally dont want people to see my errors

Foi útil?

Solução

Php DateTime's are directly comparable using < > =. The only issue I can think of is in different tinezones, so I suggest you to try prinr_r($dateNOW), and also for the other two variables, and see the output (instead of formatting the results with ->format, which may be timezone dependant.

You can the set datetime objects to the same timezone with

$dateEnd->setTimezone(new DateTimezone("your timezone"))

Or compare timestamps retrieved with

$dateEnd->getTimestamp()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top