GSM: Storing time and date from AT+CCLK

I am about to get time and date from the GSM and send as an SMS but I have a problem.
Can you tell me where did I go wrong? or do I miss something...

this is my code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

String datetime;

int inByte = 0;
volatile byte var = 0;
int state = 0;
const int pin0 = 2;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(pin0), ON, CHANGE);
  pinMode(13, OUTPUT);
}

void loop() {
  inByte = digitalRead(pin0);

  if (inByte == HIGH) {
    if ( var == 0 ) {
      state = 1;
      var = 1;
    }
  }
  else if (inByte == LOW) {
    if (var == 0) {
      state = 2;
      var = 1;
    }
  }

  switch (state) {
    case 0: //for high
      digitalWrite(13, HIGH);
      break;
    case 1: //for rising
      sendSMS();
      state = 0;
      break;
    case 2: //for falling
      sendSMS();
      state = 3;
      break;
    case 3: //for low
      digitalWrite(13, HIGH);
      delay(750);
      digitalWrite(13, LOW);
      delay(750);
      break;
    default:
      break;
  }
}

void getTime() {
  mySerial.listen();
  String mystr = "";
  int i = 0;
  mySerial.println("AT+CCLK?");
  delay(500);
  while (mySerial.available() > 0) {
    mystr += char(mySerial.read());
  }
  int x = mystr.indexOf(String('"')) + 1;
  int y = mystr.lastIndexOf(String('"'));
  datetime = mystr.substring(x, y);
  Serial.println(datetime);
  delay(500);
}

void sendSMS() {
  getTime();
  mySerial.listen();
  delay(1500);
  mySerial.println("AT+CMGF=1"); //because we want to send the sms in text mode
  delay(1500);
  mySerial.println("AT+CMGS=\"+6491293912\"\r"); //change to the phone number using
  delay(1500);
  mySerial.println("xXxXXxxX"); //content of the message
  mySerial.println("Date/Time:");
  delay(100);
  mySerial.println(datetime);
  delay(200);
  mySerial.println((char)26); //the ASCII code of the ctrl+z is 26
  delay(1000);
}

void ON() {
  var = 0;
}

my code send SMS everytime the pin 2 changes state.
if I turn on the system for the first time it prints the right time.

18/03/14,13:06:51-22

but when the state change, it makes this kind of output:

18/03/14,13:06:51-22

OK

> 
>
int inByte = 0;

It is stupid to use a type in a name that is not the type of the named variable.

  attachInterrupt(digitalPinToInterrupt(pin0), ON, CHANGE);

Why are you using an interrupt to read the switch?

  inByte = digitalRead(pin0);

And also reading the pin in loop()?

var equal 0 simply means that the interrupt happened. It says nothing about whether the switch was pressed or released. Why?

It is confusing trying to follow the logic in your code, since there are no comments and you are polling the pin as well as triggering interrupts.

  int x = mystr.indexOf(String('"')) + 1;
  int y = mystr.lastIndexOf(String('"'));

This is stupid. The indexOf() and lastIndexOf() methods are overloaded to take a String or a char. There is NO reason to make a String of a char.

You don't need to keep calling listen(), when there is only one instance of the SoftwareSerial class. That instance is always listening.

but when the state change, it makes this kind of output:

So, what is the problem? EVERY AT command you send generates a response. You fail to read any of them except for the one where you ask for the time. Then, when you read, you also get the responses to all the previous commands. Why should that come as any kind of surprise?