How to Find the Last Day of a Month in PHP

Start PHP in interactive mode.
$ php -a

Find the last day using the current UNIX timestamp.

The Procedural Style
php > echo date('t', time());
31

The Object-Oriented Style
php > $date = new DateTime();
php > echo $date->format('t');
31

Find the last day using date.

The Procedural Style
php > echo date('t', mktime(0, 0, 0, 7, 1, 2011));
31
php > echo date('t', mktime(0, 0, 0, 2, 1, 2011));
28
php > echo date('t', mktime(0, 0, 0, 2, 1, 2012));
29

The Object-Oriented Style
php > $date = new DateTime();
php > echo $date->format('t');
31
php > $date = new DateTime('2011-02-01');
php > echo $date->format('t');
28
php > $date = new DateTime('2012-02-01');
php > echo $date->format('t');
29

No comments:

Post a Comment