简体   繁体   English

如何使用php获取每月的最后一天

[英]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. 我正在尝试使用strtotime获得月份的最后一天。我不知道下面的代码有什么问题。 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; 您的最后两个date()调用完全不基于您先前计算的时间戳; 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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