Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last day of the month? [duplicate]

Tags:

Possible Duplicate:
PHP last day of the month

Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?

$start = new DateTime('2012-02-01'); $end   = clone $start;  // Interval = last day of the month minus current day in $start $interval = $start->getLastDayOfMonth() - intval($start->format('j')); $end->add(new DateInterval('P' . $interval . 'D')); 

EDIT: thanks, voted to close, it's a duplicate, sorry for asking...

like image 366
gremo Avatar asked Jan 18 '12 15:01

gremo


People also ask

How do I get the last day of the current month in C#?

DateTime createDate = new DateTime(year, month, 1). AddMonths(1). AddDays(-1);

How do you find the first and last day of the current month?

To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates. Copied! const now = new Date(); const firstDay = new Date(now.


2 Answers

The php date function gives you the number of days in the month with 't'

date("t"); 

See: http://php.net/manual/en/function.date.php

like image 80
Preau Avatar answered Dec 16 '22 10:12

Preau


It's simple to get last month date

echo date("Y-m-t", strtotime("-1 month") ) ; echo date("Y-m-1", strtotime("-1 month") ) ; 

at March 3 returns

2011-02-28 2011-02-1 
like image 23
MartijnG Avatar answered Dec 16 '22 09:12

MartijnG