PHPの日時の取得方法と次の月の表示について

このQ&Aのポイント
  • PHPで日時を正しく取得する方法について教えてください。現在のコードで1970年の月が表示されてしまっています。
  • また、指定した日時の次の月を表示する方法も教えていただきたいです。現在のコードでは正しい結果が得られません。
  • お力をお貸しいただけると助かります。よろしくお願いいたします。
回答を見る
  • ベストアンサー

PHPの日時の取得について

PHPの日時を取得するのに詰まっております。 どなたかお助けお願いいたします! やりたいことは下記です。 今月(例:2013.03)の次の月を表示→12ヶ月分繰り返す。 【めざす完成型】 今月2013.03の1か月後は2013.03 今月2013.03の2か月後は2013.04 今月2013.03の3か月後は2013.05 今月2013.03の4か月後は2013.06 今月2013.03の5か月後は2013.07 今月2013.03の6か月後は2013.08 今月2013.03の7か月後は2013.09 今月2013.03の8か月後は2013.10 今月2013.03の9か月後は2013.11 今月2013.03の10か月後は2013.12 今月2013.03の11か月後は2014.01 今月2013.03の12か月後は2014.02 現在のコード $i = 1; $cumonth = date("Y.m", strtotime(date("Y-m-01"))); while($i <= 12){ $plmonth += strtotime("+1 month", $cumonth); $plmonth_ts = date("Y.m", $plmonth); echo '今月'.$cumonth.'の'.$i.'か月後は'.$plmonth_ts.'<br />'; $i++; } ↓ 表示 ↓ 今月2013.03の1か月後は1970.02 今月2013.03の2か月後は1970.03 今月2013.03の3か月後は1970.04 今月2013.03の4か月後は1970.05 今月2013.03の5か月後は1970.06 今月2013.03の6か月後は1970.07 今月2013.03の7か月後は1970.08 今月2013.03の8か月後は1970.09 今月2013.03の9か月後は1970.10 今月2013.03の10か月後は1970.11 今月2013.03の11か月後は1970.12 今月2013.03の12か月後は1971.01 ※初期の値が1970.01になっています…。 そこでこちらに変えたところ↓ $i = 1; $cumonth = date("Y.m"); $cumonth_ts = strtotime(date("Y-m-01")); while($i <= 20){ $plmonth += strtotime("+1 month", $cumonth_ts); $plmonth_ts = date("Y.m", $plmonth); echo '今月'.$cumonth.'の'.$i.'か月後は'.$plmonth_ts.'<br />'; $i++; } ↓ 表示 ↓ 今月2013.03の1か月後は2013.04 今月2013.03の2か月後は2056.06 今月2013.03の3か月後は2099.09 今月2013.03の4か月後は2142.12 今月2013.03の5か月後は2186.03 今月2013.03の6か月後は2229.06 今月2013.03の7か月後は2272.09 今月2013.03の8か月後は2315.12 今月2013.03の9か月後は2359.03 今月2013.03の10か月後は2402.06 今月2013.03の11か月後は2445.09 今月2013.03の12か月後は2488.12 ※最初(1ヶ月後)は合っているのですが、2ヶ月後以降がおかしくなってしまいました(涙) どうぞよろしくお願い申し上げます。

  • PHP
  • 回答数1
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • LancerVII
  • ベストアンサー率51% (1060/2054)
回答No.1

こんにちは。 目指す完成系のリストが1ヶ月後じゃないのは間違いでしょうか? 今月2013.03の1か月後は2013.04 今月2013.03の2か月後は2013.05 今月2013.03の3か月後は2013.06 今月2013.03の4か月後は2013.07 今月2013.03の5か月後は2013.08 今月2013.03の6か月後は2013.09 今月2013.03の7か月後は2013.10 今月2013.03の8か月後は2013.11 今月2013.03の9か月後は2013.12 今月2013.03の10か月後は2014.01 今月2013.03の11か月後は2014.02 今月2013.03の12か月後は2014.03 こう出力したいのであれば $i = 1; //本日(文字列にはしない) $cumonth = strtotime(date("Y-m-01")); while($i <= 12){ // $iヶ月を本日に加算して、文字列を返す $plmonth_ts = date("Y.m", strtotime("+$i month", $cumonth)); // ここで初めて今月を文字列に echo '今月'.date("Y.m",$cumonth).'の'.$i.'か月後は'.$plmonth_ts.'<br />'; $i++; } こうなります。 dateで戻ってくる型等をマニュアルで確認してみると何故動かないか見えてくると思います。

marlmalmal
質問者

お礼

素早いご回答ありがとうございました! ばっちりやりたいことが表示されました。 コードも理解することができました。 まだまだ勉強不足ですが、頑張ります。 わかりやすい解説ありがとうございましたm(_ _)m

関連するQ&A

  • strtotime 前月・前々月の表示

    お世話になります。 strtotime を使って下記のように前月・前々月の表示をさせています ーーーーーーーーーーーーーーーー <?php for($i = -2 ; $i <= 0 ; $i++){ $year = date("Y",strtotime("$i month")); $month = date("m",strtotime("$i month")); print $year . "年" . $month . "月<br>"; } ?> ーーーーーーーーーーーーーーーー 7月31日にその表示をさせたら 2007年05月 2007年07月 2007年07月 と表示されてしまいました。 今月(8月)に入ってからは こちらの意図するとおりの表示に戻っていました 何か strtotime の使い方等間違っているのでしょうか? ご指導、宜しくお願い致します。

    • ベストアンサー
    • PHP
  • PHPで指定した日付の1ケ月前表示

    MySQLにある日付を拾ってきて表示しています。 <?php echo $row_target_date['ap_date']; ?> 上記のPHPでは、MySQLのテープルのap_dateカラムにある日付を拾ってきて表示するようにしています。 この1ケ月前を表示したいと考えています。 <?php echo "1月前:" . date("Y/m/d",strtotime("-1 month" ,strtotime("echo $row_target_date['ap_date']"))) . "<BR>"; ?> では表示できませんでした。 表示方法についてご指導いただきたくお願い致します。

    • ベストアンサー
    • PHP
  • 変数で来た指定日に対して、+3ヶ月、+6ヶ月、+12ヶ月を計算する方法

    変数で来た指定日に対して、+3ヶ月、+6ヶ月、+12ヶ月を計算する方法が知りたいです。 こんな感じで、当日に対して+する事は知っているのですが echo "1週間後 = " . date("Y/m/d",strtotime("+1 week")); echo "1ヶ月後 = " . date("Y/m/d",strtotime("+1 month")); echo "1年後  = " . date("Y/m/d",strtotime("+1 year")); 当日ではなく、変数できた指定の年月日に対して足す方法が解らなくて困っています。 $data03 = "2010-10-01"; //$data04 = strtotime("+3 month",$data03); $data04 = strtotime($data03,"+3 month"); //$data04 = $data03 + strtotime("+3 month"); echo '3ヶ月後:'.$data04.'<br />'; 結果 3ヶ月後:1285858800 strtotime関数のマニュアルは見ましたが良くわかりません… まったく違う方法でも良いのでお願いします。 いろいろ検索しましたが、当日からの計算方法しか見付かりませんでした。 宜しくお願い致します。

    • ベストアンサー
    • PHP
  • strtotimeについて

    指定された月の翌月を返したく以下のようにしました。 // 200909の場合翌月の200910を返す。これは問題ありません echo date('Ym', strtotime('+1 month')); // 問題は以下から // 指定の月をセット $str = '200910; // どちらの場合も200910を返す echo date($str, strtotime('+1 month')); echo date($str, strtotime('-1 month')); このような方法で$strが200911であれば200912を返し200912であれば201001を返したのですが どのようにすれば可能でしょうか?

    • 締切済み
    • PHP
  • PHPのカレンダーに曜日をつけられづに困っています。

    <?php // 日付 if (!isset($hiduke)) { $hiduke = date("Y-m-d"); } if (!isset($yyyy)) { $yyyy = date("Y", strtotime($hiduke)); } if (!isset($mm)) { $mm = date("m", strtotime($hiduke)); } if (!isset($dd)) { $dd = date("d", strtotime($hiduke)); } // 表示ページ if (isset($_GET["act"])) { $act = $_GET["act"]; } if (!isset($act)) { $act = ""; } // ================================================= //  処理開始 ?> <?php conndb();?> <html> <head> <meta http-equiv="content-type" content="text/html; charset=Shift_JIS"> <title>予約システム</title> </head> <body> <h2>予約システム</h2> <?php // -- ページ内容の表示 switch ($act) { case "upd": // 更新 gamen_upd(); break; case "updconf": // 更新確認 gamen_updconf(); break; case "delconf": // 削除確認 gamen_delconf(); break; case "det": // 詳細表示 gamen_detail(); break; default: gamen_iti(); } ?> </body> </html> <?php closedb(); ?> <?php // ================================================= // ----- 一覧表示画面 function gamen_iti() { global $conn; global $hiduke; global $yyyy; //年 global $mm; //月 $array = cnv_formstr($_POST); extract($array, EXTR_SKIP); if (isset($kbn)) { if ($kbn == "upd" or $kbn == "del") { update_yoyaku($kbn); } } $sql = "SELECT * FROM jikan ORDER BY timeid"; $res = mysql_query($sql, $conn) or die("データ抽出エラー"); ?> <form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>"> <table border="1"> <tr> <td><?php setmonth(); ?></td> <td><input type="submit" name="sub" value="予約表示"></td> </tr> </table> <input type="hidden" name="act" value="iti"> <input type="hidden" name="kbn" value="go"> </form> <p><?php echo date("Y 年 m 月 ", mktime(0, 0, 0, $mm, 1, $yyyy)) ?>予約一覧 <table border="1"> <tr bgcolor="#ffd700"> <td>日付</td> <?php // 時間表示 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { $timeid = $row["timeid"]; $jikan = cnv_dispstr($row["jikan"]); ?> <td><?php echo $jikan ?></td> <?php } ?> </tr> <?php //カレンダー $lastd = date("d", mktime(0, 0, 0, $mm + 1, 0, $yyyy)); for ($i = 1; $i <= $lastd; $i++) { ?> <tr> <?php $ts = mktime(0, 0, 0, $mm, $i, $yyyy); $ymd = date("Y/m/d" ,$ts) ; $ymdstr = strftime("%Y年%m月%d日(%a)", $ts); ?> <td><?php echo $ymd ?></td> <?php   <?php } ?> </table> <?php } // ================================================= // ----- 年月選択リスト表示 function setmonth() { global $yyyy; global $mm; $array = cnv_formstr($_POST); extract($array, EXTR_SKIP); echo "<table><tr><td>"; // 年 echo "<select name=\"yyyy\">"; for ($i = 2008; $i <= 2025; $i++) { echo "<option"; if ($i == $yyyy) { echo " selected "; } echo ">$i"; } echo "</select>年"; // 月 echo "<select name=\"mm\">"; for ($i = 1; $i <= 12; $i++) { echo "<option"; if ($i == $mm) { echo " selected "; } echo ">$i"; } echo "</select>月"; echo "</td>"; echo "</tr></table>"; } ?> と参考書を見ながら、作っています。 PHPはまったくの初心者です。カレンダーの日付に曜日をつけられづに困っています。コードを含め詳しくをしえてもらえると助かります。よろしくお願いします。

    • 締切済み
    • PHP
  • RSSブロガーの日時表示について

    レッツPHPさんのRSSブロガーを利用しております。 http://loglog.jp/~php/net/ 機能に日時を表示させたく以下のソースを記入しました 27行目付近より for ($i=0; $i<$num; $i++) { $val = $r->getItems(); $title = mb_convert_encoding(strip_tags($val[$i]['title']), $code, "UTF-8,EUC-JP,SJIS"); $desc = mb_convert_encoding(strip_tags($val[$i]['description']), $code, "UTF-8,EUC-JP,SJIS"); $link = htmlspecialchars($val[$i]['link']); $date = $val[$i]['dc:date']; $time = (isset($val[$i]['pubdate'])) ? strtotime($val[$i]['pubdate']) : strtotime(str_replace("T", " ", substr($val[$i]['dc:date'], 0, 19))); $koushin = date("Y/m/d", $time); // 内容 echo <<<B <tr bgcolor="#ffffff"><td><small> $koushin <a href="$link" target=_new>$title</a><br> $desc </small></td></tr> B; } これで日時も表示が出来たのですが、RSSブロガーの最大表示件数を5件にします。 例えば1件ブログなどで書き込みをこのRSSブロガーの部分で表示をすると1件は日時とタイトルが表示されるのですが、残りの4件の日付が1970/01/01 と表示されタイトル無しの表示が4つ並びます。 ↓このような感じ 2008/02/14 テスト 1970/01/01  (空白) 1970/01/01  (空白) 1970/01/01  (空白) 1970/01/01  (空白) これだと見た目が悪いので空白部分の日時と空白部分は表示させたくないのですが、何か良い方法はございますか宜しくお願い致します。

    • ベストアンサー
    • PHP
  • 10月31日の翌月は11月ではなく12月となる

    あるプログラムで以下の処理があるのですが、 本日動作したログを確認したところ、 結果が"2014-12-01"となっており、 本来取得したかった"2014-11-01"ではありませんでした。 15日以上は来月の1日、14日未満は今月の1日を正しく返す処理にしたいのですが、 修正をする際の代替案がありましたらご教示お願いします。 <処理> function hoge() { if (15<= date('d')) { return date('Y-m-01', strtotime('+1 month')); } else { return date('Y-m-01'); } } echo hoge()."\n"; 以上

    • ベストアンサー
    • PHP
  • php storetotime 最終日の前日

    たとえば、2か月前の最終日を求めるには date('Ymd', strtotime( 'last day of -2 month' )) で求められますが、その前日を求めるにはどうすれば良いでしょうか? date('Ymd', strtotime( 'last -2 day of -2 month' ))

    • ベストアンサー
    • PHP
  • PHPで取得した日時データをDBに挿入する方法

    こんにちは。 以下のようにMariaDBのコマンドで「sysdate()」を使用して日時を入力しているのですが、PHPで同様のことができずに困っています。 ご教示頂けませんか。 宜しくお願い致します。 【MariaDB】 MariaDB [tweet01]> insert tweet_tbl(account, contents, input_datetime) values('account01', 'contents01', sysdate()); MariaDB [tweet01]> select * from tweet_tbl; +----+-----------+------------+---------------------+ | id | account | contents | input_datetime | +----+-----------+------------+---------------------+ | 6 | account01 | contents01 | 2016-04-19 00:24:36 | +----+-----------+------------+---------------------+ 【PHP】 $account = $_REQUEST['account']; $contents = $_REQUEST['contents']; $input_datetime = "echo date( "Y-m-d H:i:s" )"; $result = mysqli_query($con, "insert into tweet_tbl(`account`, `contents`, `input_datetime`) values('$account', '$contents', '$input_datetime')"); 以下の行をうまく修正できれば解決しそうな気がしているのですが… $input_datetime = "echo date( "Y-m-d H:i:s" )";

    • ベストアンサー
    • PHP
  • if構文の中で更に条件分岐をしたい

    ニュース記事を下記のようにカテゴリ別に表示しています。 while($rec = mysql_fetch_array($rs, MYSQL_ASSOC)){ if($rec['category'] == 1){ echo '<a href="'.$rec['url'].'" target=_blank>'.$rec['title'].'</a><br>'; } elseif($rec['category'] == 2){ echo '<a href="'.$rec['url'].'" target=_blank>'.$rec['title'].'</a><br>'; } elseif($rec['category'] == 3){ echo '<a href="'.$rec['url'].'" target=_blank>'.$rec['title'].'</a><br>'; } } 24時間以内の記事にはNEWを表示したいのですが、どのように組み込んだらいいのか分かりません。 下記がNEWを表示するためのコードです。 while($rec = mysql_fetch_array($rs, MYSQL_ASSOC)){ $ts = strtotime($rec['date']); $tsi = (time()-$ts); if($tsi<86400){ echo new.'<br>'; } else{ echo '<br>'; } } このようにifの中で更に条件分岐したい場合、どのようなやり方があるのでしょうか?

    • ベストアンサー
    • PHP