Last day this month php

How to find the last day of the month from date?

t returns the number of days in the month of a given date (see the docs for date ):

$a_date = "2009-11-23"; echo date("Y-m-t", strtotime($a_date)); 

@Mugunth If you are working with dates 24 years in the future you may run into problems sooner than 2038, however, servers are already moving over to 64-bit architecture which will give us about 292 billion years to correct the problem.

in procedural -> $date1 = $year.’-‘.$month; $d = date_create_from_format(‘Y-m’,$date1); $last_day = date_format($d, ‘t’);

@victor you should bump your comment as answer for the sole reason that it results in readable and understandable code. Clearly using Y-m-t would work as well but who without looking at the docs would know what «t» is?

If you’re using a 32-bit system, then the strtotime() code will fail after the year 2038 due to the Year 2038 problem.

For example, on a 32-bit system, this code will echo 1970-01-31 :

$a_date = "2040-11-23"; echo date("Y-m-t", strtotime($a_date)); 

If you need your code to support 32-bit systems, then you should instead use the DateTime function:

$d = new DateTime('2040-11-23'); echo $d->format('Y-m-t'); 

That code will correctly output 2040-11-30 on both a 32-bit or 64-bit system.

I will be 71 and retired by then, so not a problem for me! j/k But seriously folks — heed this warning!

why is this alarming fact about strtotime not mentioned in the php docu php.net/manual/de/function.strtotime.php ?

I know this is a little bit late but i think there is a more elegant way of doing this with PHP 5.3+ by using the DateTime class :

$date = new DateTime('now'); $date->modify('last day of this month'); echo $date->format('Y-m-d'); 

You might want to add $date->modify(‘last day of this month’)->setTime(23,59,59); if you would like to use this dateTime for comparison where time matters. (This gives you the last second of the month)

@John you are wrong. the last day is calculated using the $date object and not the system date. Try it by changing the first line with this : $date = DateTime::createFromFormat(‘m/d/Y’, ‘1/10/2014’);

If you were to think or at least try what I asked you to do you would see that this month means the value of the month in the DateTime object not the system’s date. If you can’t type/run php code this might help : sandbox.onlinephpfunctions.com/code/…

There is also the built in PHP function cal_days_in_month()?

«This function will return the number of days in the month of year for the specified calendar.» http://php.net/manual/en/function.cal-days-in-month.

echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); // = 30 

This answer is much simpler and appropriate than the use of strtoTime. I don’t see any downsides. To the top you go.

$week_start = strtotime('last Sunday', time()); $week_end = strtotime('next Sunday', time()); $month_start = strtotime('first day of this month', time()); $month_end = strtotime('last day of this month', time()); $year_start = strtotime('first day of January', time()); $year_end = strtotime('last day of December', time()); echo date('D, M jS Y', $week_start).'
'; echo date('D, M jS Y', $week_end).'
'; echo date('D, M jS Y', $month_start).'
'; echo date('D, M jS Y', $month_end).'
'; echo date('D, M jS Y', $year_start).'
'; echo date('D, M jS Y', $year_end).'
';

The most elegant for me is using DateTime

I wonder I do not see DateTime::createFromFormat , one-liner

$lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t"); 

Try this , if you are using PHP 5.3+,

$a_date = "2009-11-23"; $date = new DateTime($a_date); $date->modify('last day of this month'); echo $date->format('Y-m-d'); 

For finding next month last date, modify as follows,

 $date->modify('last day of 1 month'); echo $date->format('Y-m-d'); 

This is the correct answer. Just to complement your answer, you can also use it like this $date = new DateTime(‘last day of 2022-06’); .

You could create a date for the first of the next month, and then use strtotime(«-1 day», $firstOfNextMonth)

an alternative method using the same approach is to give mktime the 0th day of the next month mktime(0, 0, 0, $month+1, 0, $year);

As to me, usage of strtotime() and mktime() is discouraged bec. of known bugs like date determination on the edge of the months and leap year calaculations. Since DateTime is available, you should use this class to avoid any problems in future. Like said many times before me, both these function strtotime() and mktime() will fail after 2038. That’s why I downvoted .

@PaulT.Rawkeen This answer is more than 4 years old. As with anything on the internet, age should be a factor when gauging relevance and usefulness. Further more, IMHO, the halving time for anything related to technology is particularly short.

@nikc.org I agree, especially regarding time of life for all technical solutions and ways of solving problems. In this particular case, answer has relatively good number of upvotes and probably, someone (junior programmer) will consider this answer as acceptable solution bec. it is short and elegant 🙂 and he will probably get the expected result, but as time goes by, this way of solving such kind of problems becomes tricky and we should warn about such facts when its possible. Kind regards.

Источник

how to get the first and last days of a given month

I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a ‘Y-m-d’ parameter format, but I don’t know how can I get the last day of the given month date.

$query_date = '2010-02-04'; list($y, $m, $d) = explode('-', $query_date); $first_day = $y . '-' . $m . '-01'; 

10 Answers 10

You might want to look at the strtotime and date functions.

what if $query_date comes from php as ‘d-m-Y’ format: ’02-04-2010′? On my tests, it goes fine, but maybe I didn’t test all the situations. Do I have to convert to Y-m-d, or strtotime, understand it anyway.

The «d-m-Y» format is supported. The supported formats are listed at http://ca2.php.net/manual/en/datetime.formats.date.php.

I’ve pretty much searched the whole web for so many times. This is an ingenious solution. Thanks Francois.

I know this question has a good answer with ‘t’, but thought I would add another solution.

$first = date("Y-m-d", strtotime("first day of this month")); $last = date("Y-m-d", strtotime("last day of this month")); 

This solution is wrong, because the question was about a «given month», but your answer only works for the current month.

Replacing «this month» by desired month (like $date->format(‘F’) or (new \DateTime(‘2010-02-04’)->format(‘F’) in this case) does the trick to

Try this , if you are using PHP 5.3+, in php

$query_date = '2010-02-04'; $date = new DateTime($query_date); //First day of month $date->modify('first day of this month'); $firstday= $date->format('Y-m-d'); //Last day of month $date->modify('last day of this month'); $lastday= $date->format('Y-m-d'); 

For finding next month last date, modify as follows,

 $date->modify('last day of 1 month'); echo $date->format('Y-m-d'); 

Источник

In PHP, is there an easy way to get the first and last date of a month?

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?

12 Answers 12

$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year)); $last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year)); 

See date() in PHP documentation.

First day is always YYYY-MM-01, isn’t it? Example: date(«Y-M-d», mktime(0, 0, 0, 8, 1, 2008))

Last day is the previous day of the next month’s first day:

$date = new DateTime("2008-09-01"); $date->modify("-1 day"); echo $date->format("Y-m-d"); 

The first day of the month is always 1. So it will become

the last day can be calculated as:

date ('Y-m-d', mktime(0,0,0,MM,01,YYYY)); 

Last is a little trickier, but not much.

date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY)); 

If I remember my PHP date stuff correctly.

**edit — Gah! Beaten to it about a million times.

Last day should have been

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1 
"; $num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y")); echo "Monthe End - " . $monthEnd = date("Y-m-".$num); ?> 

The easiest way to do this with PHP is

$dateBegin = strtotime("first day of last month"); $dateEnd = strtotime("last day of last month"); echo date("MYDATEFORMAT", $dateBegin); echo "
"; echo date("MYDATEFORMAT", $dateEnd);
$dateBegin = strtotime("1/1 last year"); $dateEnd = strtotime("12/31 this year"); 

By the way @ZombieSheep solution

date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY)); 

does not work it should be

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1 

Of course @Michał Słaby’s accepted solution is the simplest.

Just to verify that I didn’t miss any loose ends:

$startDay = 1; if (date("m") == 1) < $startMonth = 12; $startYear = date("Y") - 1; $endMonth = 12; $endYear = date("Y") - 1; >else < $startMonth = date("m") - 1; $startYear = date("Y"); $endMonth = date("m") - 1; $endYear = date("Y"); >$endDay = date("d") - 1; $startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear)); $endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear)); 

try this to get the number of days in the month:

$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y)); 

Example; I want to get first day and last day of current month.

$month = (int) date('F'); $year = (int) date('Y'); date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last 

When you run this for instance at date 2015-01-09, the first and last values will be sequentially;

From here(get next month last day) that is marked as duplicated, so i can’t add comment there, but people can got bad answers from there.

Correct one for last day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t')); 

Correct one for first day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01')); 

Code like this will be providing March from January, so that’s not what could be expected.

echo ((new DateTime())->modify(‘+1 month’)->format(‘Y-m-t’));

proper way to build a relative date from now is:

//bad example - will be broken when generated at 30 of December (broken February) echo date("Y-m-d", strtotime("now"))."\n"; echo date("Y-m-d", strtotime("now + 1 month"))."\n"; echo date("Y-m-d", strtotime("now + 2 month"))."\n"; echo date("Y-m-d", strtotime("now + 3 month"))."\n"; //good example, you can change first day to last day or any day echo date("Y-m-d", strtotime("first day of this month"))."\n"; echo date("Y-m-d", strtotime("first day of next month"))."\n"; echo date("Y-m-d", strtotime("first day of +2 month"))."\n"; echo date("Y-m-d", strtotime("first day of +3 month"))."\n"; 
2021-12-30 2022-01-30 2022-03-02 2022-03-30 2021-12-01 2022-01-01 2022-02-01 2022-03-01 

Источник

Читайте также:  Машинное обучение нейронные сети python
Оцените статью