PHP 現在の月を取得するサンプルコード

環境
PHP 8.1.2
Ubuntu 22.04.1 LTS

構文
//現在の月を0埋めあり 01-12
$currentMonth = date('m’);
//現在の月を0埋めなし 1-12
$currentMonth = date('m’);
現在の月を取得するには、date()を使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$currentMonth = date('m');
echo $currentMonth;
?>
<?php $currentMonth = date('m'); echo $currentMonth; ?>
<?php
$currentMonth = date('m');

echo $currentMonth;
?>

結果 11

PHP

Posted by arkgame