Php get the last day of this month

I want to display the last and first date of this month of the current year. I use this code but not working

$month='02';
$first_day_this_month = date('Y-'.$month.'-01'); // hard-coded '01' for first day
$last_day_this_month  = date('Y-'.$month.'-t');

echo $first_day_this_month;print'<->';echo $last_day_this_month;

my conclusion shows

2015-02-01<->2015-02-31

But it will be 2015-02-01<->2015-02-28

+4
source share
2 answers

I had this problem with PHP before, try the following path:

$dateToTest = "2015-02-01";
$lastday = date('t',strtotime($dateToTest));
+8
source

There are many ways to do this, I give you two answers \ ideas:

1- Try using the strtotimePHP function ( http://php.net/manual/es/function.strtotime.php )

Something like date("Y-m-d", strtotime("last day of this month"));either the first day ... or any month.

2- Another way to use this:

First day:

date("Y-m-d", mktime(0, 0, 0, *YOUR MONTH PARAM*,1 ,date("Y")));

Last day:

date("Y-m-d", mktime(0, 0, 0, *YOUR MONTH PARAM*+1,0,date("Y")));

mktime :

http://php.net/manual/es/function.mktime.php

!

+1

Source: https://habr.com/ru/post/1611702/


All Articles