简体   繁体   中英

how to get the last day of the month using php

I'm trying to get the last day from the month using strtotime .I don't know what wrong with the below code. Can some one shoot out the issue ?

$string = 'Jun';
$month_number = date("m",strtotime($string));
$calcstart_date = date('Y-'.$month_number.'-01').' 00:00:00';
echo $calcend_date = date('Y-'.$month_number.'-t').' 11:59:00';

Your last two date() calls aren't based in any way on the timestamp you computed earlier; those will just use the number of days in the current month.

You want:

$time = strtotime($string);
$calcstart_date = date('Y-m-\0\1', $time);
$calcend_date = date('Y-m-t', $time);

And if you need the month number independently you can still do

$month_number = date('m', $time);

Finally if you'd like just the last day number by itself, that would be:

$number_of_days = date('t', $time);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM