2016-03-27 5 views
1

今日の今日の日付(m-d-Y)を取得して30日を追加したいと考えています。私はこれをmySQLデータベースに入れます。それから毎日、それは14日に近いときに何日残ったかを言うでしょう。phpどのように日付を減算または追加するには?

$today = strtotime(date('Y-m-d H:i:s')); 
$expireDay = date('m-d-Y', strtotime("+30 days")); 
$timeToEnd = $expireDay - $today; 
if ($timeToEnd <= strtotime("$expireDay - 14 days")// this is not correct syntax 
{ 
echo "less than 14 days"; 
echo $timeToEnd('d')//here is where I am having problems with my syntax 
} else { 
echo "more than 14 days"; 
} 
+0

'$ expireDay'は整数ではなく文字列です。 'strtotime'を使うと、それを使ってデータを得ることができます。 'strtotime(date( 'Y-m-d H:i:s'))'は 'time()'でもかまいません。 – chris85

+0

この質問はほぼ毎日尋ねられています。 http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php – Axalix

答えて

2

$todayは、このコードがうまくいくかどうかをテストするためのものです。あなたのコードは、あなたができない文字列を減算しようとしていました。 date('m-d-Y', strtotime("+30 days"));は、日付文字列03-27-2016です。これは、strtotimeの整数が必要です。

$today = time(); 
$expireDay = strtotime("+30 days"); 
$daysleft = (($expireDay - $today)/86400); 
if ($daysleft <= 14) { 
    echo "less than 14 days"; 
    echo $daysleft; 
} else { 
    echo "more than 14 days"; 
} 
+0

私はこれで一日中過ごしています。本当にありがとう! :) – DDJ

関連する問題