2010-08-05 34 views
0

目標C/iPhone中PHP的strtotime目標C/iPhone中是否有與PHP的strtotime等效(或者模糊相似)的目標C/iPhone

的strtotime例如可以瞭解以下時表達式(從PHP文檔):

  • 回波的strtotime( 「現在」), 「\ n」 個;
  • echo strtotime(「2000年9月10日」),「\ n」;
  • echo 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」;

回答

0

這裏是您查詢的第一和第二部分代碼:

  • 回聲的strtotime( 「現在」), 「\ n」;
  • echo strtotime(「2000年9月10日」),「\ 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]); 

顯然ALLOC-ED對象需要釋放。

而這裏的一篇文章,可以與其他人幫助:

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

+1

我想你和邁克爾錯過了這一點,因爲你不需要知道輸入格式(這是strtime的設計)。 只是我猜測答案是「沒有等價的」。 – Jonny 2010-08-06 06:53:49

+0

好Objective-C不是像PHP這樣的腳本語言,所以它不能做你想做的事情。 答案是 - 有一個等價的,但它是一個靜態類型的等價物,它有點冗長。 – Matt 2010-08-06 08:40:39

1

[NSDate +dateWithString]做你想做的,我想。

+0

它存在於OS X,但不是iOS。無論如何,很高興知道。 – Jonny 2013-01-01 08:00:08

0

突然我想起了「PHPJS」項目。還有的strtotime的JavaScript的端口位置:

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

我還沒有測試它(這意味着,將它移植到一個目標C函數或類似的),所以我不知道有多好,它實際上執行與原始時間相比。可能有時間檢查,以後...