mardi 4 août 2015

Getting date value with find() and Checking if the date is < 30 mins

I try to make an app. that sends e-mail to the user. Whenever i run the code, the current time and date is saved to the database under the name of "created" (column). There are 2 conditions. I could make the first one in that if loop. Second condition is sending e-mail after 30 mins from the previous e-mail. To do this, I try to get those saved values by using find() and compare them to do 30 mins condition. (Btw I am very newbie at programming. I ve been learning for 3 weeks)

Here is the code;

class TemperatureReading extends AppModel {

    function afterSave($created, $options = array()) {

    $temperature = $this->data['TemperatureReading']['temperature'];

    $result = $this->Location->find('first' , array(
            'conditions' => array(
                    'Location.id' => $this->data['TemperatureReading']['location_id']
            )));

    $high_threshold = $result['Location']['high_threshold'];

    $low_threshold = $result['Location']['low_threshold'];

    $created = $this->data['TemperatureReading']['created'];

    $id = $this->data['TemperatureReading']['id'];

    $models_Temp_Loc = $this->find('first' , array(
            'conditions' => array(
                    'TemperatureReading.id' => $id
            )));

    $prev_created_time = $models_Temp_Loc['TemperatureReading']['created'];

// The part, where I have problems. I want to get the values of created (time and date) and compare them to check 30 mins condition. (ex: Assume that app. ran and e-mail sent for the first time at specific time. The application can be run 100 or 241 or 4144.. times in 10 mins after the first e-mail, so there are many date and time data saved in created in 10 mins to be compared for 30 mins condition.)

    $deneme = $this->find('all' , array(
        'conditions' => array(
                'created.id' => $id
                ), 
                    'condition' => array(
                        'created' <30
                )));

    //The part where i can make the first condition without problem

    if ($temperature > $high_threshold || $temperature < $low_threshold) {

        $Email = new CakeEmail('gmail');
        $Email->from(array('xxxxxxx@gmail.com' => 'My Site'));
        $Email->to('xxxxxxxxxx@gmail.com');
        $Email->subject('Temperature Warning!');
        $Email->send('Temperature is at critical value of');

    }

Excuse me for any meaningless things, i am very noob and trying to do hard things.

This is what goes on there without $deneme (wrong part). for every id, there is a date and time to be compared for the 30 min condition.

Temperature is: 12
High_threshold is: 90
Low_threshold is: 0
Created is: 2015-08-04 14:47:52
Id is: 340

Array
(
    [TemperatureReading] => Array
        (
            [id] => 340
            [created] => 2015-08-04 14:47:52
            [temperature] => 12
            [location_id] => 1
        )

    [Location] => Array
        (
            [id] => 1
            [name] => Spor Salonu
            [high_threshold] => 90
            [low_threshold] => 0
            [send_warning_to] => onurcucen@gmail.com
        )

)

2015-08-04 14:47:52

ty for any possible recommendations, comments, answers.

Aucun commentaire:

Enregistrer un commentaire