2017-12-30 2 views
1

DateTimeオブジェクトを作成して14日前に戻すように修正しました。DateTimeを今までに変更できません

本当にありがとうございます。

$sql_query_date = new DateTime("now",new DateTimeZone("America/New_York")); 
    $sql_query_date->modify("14 days ago"); 
    $start_sql = $sql_query_date->format("Y-m-d"); 

    $sql_query_date->modify("now"); 
    $end_sql = $sql_query_date->format("Y-m-d"); 
+1

はい、それはあなたが設定した日時に動作しますので。だから今日は今日の日付を設定した現在の日付を意味します – urfusion

+0

これはhttp://php.net/manual/de/datetime.formats.relative.phpで文書化されています – NineBerry

答えて

1

nowは、今日の日付ではなく、現在の日付を設定します。したがって、日付オブジェクトを再作成するか、変更値を保持して戻すことができます。

$sql_query_date = new DateTime("now",new DateTimeZone("America/New_York")); 
    $sql_query_date->modify("14 days ago"); 
    $start_sql = $sql_query_date->format("Y-m-d"); 
    print_r($start_sql); 
    $sql_query_date = new DateTime("now",new DateTimeZone("America/New_York")); 
    $end_sql = $sql_query_date->format("Y-m-d"); 
    print_r($end_sql); 

OR

$sql_query_date->modify("-14 days ago"); 
    $end_sql = $sql_query_date->format("Y-m-d"); 
    print_r($end_sql); 
関連する問題