Jonny
Jonny

Reputation: 16328

PHP's strtotime in Objective C/iPhone

Is there an equivalent (or vaguely similar) to PHP's strtotime in Objective C/iPhone?

strtotime can for example understand the below time expressions (from the php documentation):

Upvotes: 0

Views: 1197

Answers (3)

Jonny
Jonny

Reputation: 16328

All of a sudden I remembered the "PHPJS" project. There is a javascript port of strtotime here:

http://phpjs.org/functions/strtotime:554

I have yet to test it (which means, porting it to a Objective C function or similar), so I'm not sure how well it actually performs in comparison with the original strtotime. Might have time for checking that later...

Upvotes: 0

Matt
Matt

Reputation: 5661

Here is some code for the first and second of your queries:

  • echo strtotime("now"), "\n";
  • echo strtotime("10 September 2000"), "\n";
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"dd MMM yyyy"];
    NSDate *now = [[NSDate alloc] init];
    NSString *dateString = [format stringFromDate:now];
    NSLog(@"The time: %@", dateString);

    NSDate *parsed = [format dateFromString:@"10 Sep 2000"];
    NSLog(@"The time: %@", [format stringFromDate:parsed]);

Obviously the alloc-ed objects need releasing.

And here's an article that may help with the others:

http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate

Upvotes: 0

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95579

[NSDate +dateWithString] does what you want, I think.

Upvotes: 1

Related Questions