3
私はUnixタイムスタンプを持っていて、先月の名前を取得したいと思っていますe。 g。 "Ferbruary"PHPは先月の名前をタイムスタンプから取得する
$date = 1489842000;
$lastMonth = getLastMonth($date); //Ferbruary
私はUnixタイムスタンプを持っていて、先月の名前を取得したいと思っていますe。 g。 "Ferbruary"PHPは先月の名前をタイムスタンプから取得する
$date = 1489842000;
$lastMonth = getLastMonth($date); //Ferbruary
strtotimeはここにあなたの友人である:これは完全に動的たい人のための
echo Date('F', strtotime($date . " last month"));
、常に先月の名前を表示するには、コードは次のようになります。
$currentMonth = date('F');
echo Date('F', strtotime($currentMonth . " last month"));
DateTime
オブジェクトを指定されたタイムスタンプに設定し、次に'P1M'
(1か月)の間隔を減算すると、
/**
* @param {int} $date unix timestamp
* @return string name of month
*/
function getLastMonth($date) {
// create new DateTime object and set its value
$datetime = new DateTime();
$datetime->setTimestamp($date);
// subtract P1M - one month
$datetime->sub(new DateInterval('P1M'));
// return date formatted to month name
return $datetime->format('F');
}
// Example of use
$date = 1489842000;
$lastMonth = getLastMonth($date);
ここにコードを説明すると、より良い回答になります。 – Deep
このコードスニペットは歓迎されていますが、いくつかの助けを与えるかもしれませんが、* how *と* Why *がこれを解決する(説明があれば大きく改善されます)(// meta.stackexchange.com/q/114762)問題。あなたが今質問している人だけでなく、将来読者のための質問に答えていることを忘れないでください!説明を追加するためにあなたの答えを[編集]し、どんな制限と前提が適用されるかを示してください。 –
@TobySpeightの説明が追加されました –