user559142
user559142

Reputation: 12517

Sending date with ASIHTTP from iPhone app to php app

I have an ipone app which uses the following method to send data to a php file (using ASIHTTP framework):

-(void)createEntry:(NSString*)url forUser:(NSString*)email withDate:date requestToDelegate:(id)delegate{
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:@"set" forKey:@"newentry"];   
    [request setPostValue:email forKey:@"user"];    
    [request setPostValue:date forKey:@"date"];
    [request setDelegate:delegate];
    [request startSynchronous];
}

My php file looks like this:

if(isset($_POST['newentry']){
    $dbh = connect();
    $date = $_POST['date'];
    $user = $_POST['user'];
    $user = str_replace("@","at",$user);
    $user = str_replace(".","dot",$user);
    $user .= "Entries";
    $query= "INSERT INTO $user (date) VALUES ('$date')";
    mysql_query($query);

    $query= mysql_query("SELECT entryId FROM $user WHERE date = '$date'");
    $row = mysql_fetch_array($query);
    echo(row['entryId']);
}

my mysql table expects a date in the form YYYY-MM-DD. My question is how can I send a proper date format object from objective c to the php file...how do I create a compatible date?

Upvotes: 0

Views: 313

Answers (1)

Craig Stanford
Craig Stanford

Reputation: 1803

Converting dates in Objective-C is done using an NSDateFormatter

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString* dateString = [dateFormatter stringFromDate:date];
[dateFormatter release];

where dateString holds the value you want to send to your PHP server.

Upvotes: 1

Related Questions