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):
- echo strtotime("now"), "\n";
- echo strtotime("10 September 2000"), "\n";
- ech开发者_JAVA百科o strtotime("+1 day"), "\n";
- echo strtotime("+1 week"), "\n";
- echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
- echo strtotime("next Thursday"), "\n";
- echo strtotime("last Monday"), "\n";
[NSDate +dateWithString] does what you want, I think.
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
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...
精彩评论