9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで「○月●日以降にこれを切り替えて表示してほしい」と言われたら

Last updated at Posted at 2015-09-29

A.「このバナー、○月●日になったら切り替えてほしいんだけど」
B.「お正月のあいさつを元日に出して、15日過ぎたら消してほしい」
C.「○月●日×時以降に表示してほしい」

そのあたりに対応したコードをいつも忘れるのでメモ。

Aのとき
<?php if ('20151001'<=date('Ymd')) { ?>
    <img src="images/20151001.jpg" />
<?php } else { ?>
    <img src="images/20150930.jpg" />
<?php } ?>
Bのとき(1月1日から15日の間)
<?php if ('20150101'<=date('Ymd') && date('Ymd')<='20150115') { ?>
    <img src="images/20150101.jpg" />
<?php } else { ?>
    <img src="images/20151230.jpg" />
<?php } ?>
Cのとき(12月1日の9時以降の場合)
<?php if ('201512010900'<=date('YmdHi')) { ?>
    <img src="images/20151201.jpg" />
<?php } else { ?>
    <img src="images/20151130.jpg" />
<?php } ?>

こちらの記事のコメント欄で書いてあることではあるけれど。
シンプルに書きたかったので自分用メモです。
【PHP】日付比較

9
10
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?