0

前月の開始日と終了日を取得したい。

だから、今は 2012 年 7 月です。関数が 2012 年 6 月 1 日を開始として返し、2012 年 6 月 30 日を終了として返します。

これは私のコードです:

$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";

しかし、それはエコーします:

Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)

私が間違っていることは何か分かりますか?

4

5 に答える 5

1

投稿したコードの問題点に答えると、丸括弧を少し移動するだけで済みました。:)

<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>
于 2012-07-10T06:52:10.757 に答える
0
echo $firstdate= "01/".date('m')."/".date('Y') ;
 $lastdateofmonth=date('t',date('m'));
 echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;

また

$date='2012-06-05 12:00:00';

echo "End = ".date("Y-m-t",strtotime($date)); 
echo "start = ".date("Y-m-01",strtotime($date)); 
于 2012-07-10T06:58:10.510 に答える
0

これを試してください(のマニュアルを確認してくださいstrtotime

$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);
于 2012-07-10T06:47:54.810 に答える
0

次のようなことを試してください:

echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));
于 2012-07-10T06:47:58.173 に答える
0

あなた(および他の回答:X)が試したものは、少し大きすぎるようです。ジャスティスです

echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));

任意の月

// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));

// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));

続きを読むマニュアルで

于 2012-07-10T06:48:47.793 に答える