[英]I want to Get end date of February in PHP
I Used <?php $febenddate=date('Y-2-t'); ?>
我用<?php $febenddate=date('Y-2-t'); ?>
<?php $febenddate=date('Y-2-t'); ?>
But Result is 2021-02-31 , How can i get 2021-02-28 ?但是结果是2021-02-31 ,我怎样才能得到2021-02-28 ?
You can use the function cal_days_in_month()
您可以使用 function cal_days_in_month()
You need to provide the month and year for which you need to find the total no of days (or the end date)您需要提供需要查找总天数(或结束日期)的月份和年份
cal_days_in_month (int $calendar, int $month, int $year) : int
In your case:在你的情况下:
echo cal_days_in_month(CAL_GREGORIAN, 2, 2021);
You can use DateTime with a relative date expression.您可以将 DateTime 与相对日期表达式一起使用。
$dateTime = date_create('Last Day of February 2021');
To output the date in the desired format:到 output 所需格式的日期:
echo $dateTime->format('Y-m-d'); //2021-02-28
If the year is omitted, February of the current year is used.如果省略年份,则使用当年的二月。
$dateTime = date_create('Last Day of February');
To get the last day of February of the current year, first make a timestamp for any day in February—for which the 1st of February is a good choice—and get its t
value.要获得当年 2 月的最后一天,首先为 2 月的任何一天(2 月 1 日是一个不错的选择)制作时间戳,并获取其t
值。 Just writing 2
in your date
format string doesn't make it choose the t
value of February:仅在您的date
格式字符串中写入2
并不会使其选择二月的t
值:
echo date('Y-m-t', mktime(0, 0, 0, 2, 1));
See http://php.net/mktime .请参阅http://php.net/mktime 。
Try this one:试试这个:
echo date("Y-m-t", strtotime("2021-02-14"));
The "t" in "Ymt" returns number of days in given month (param set in strtotime function). “Ymt”中的“t”返回给定月份的天数(参数在 strtotime 函数中设置)。 In short: you are echoing out: Y: Year, m: Month, t: Number of days in the given month in given year.简而言之:您正在回显:Y:年,m:月,t:给定年份中给定月份的天数。
$date=date_create("2021-02-28");
echo date_format($date,"Y-m-d");
You can get the last date of Month using the below PHP Code....您可以使用下面的 PHP 代码获取月份的最后日期....
<?php
$d = new DateTime( '2021-02-01' );
echo $d->format( 'Y-m-t' );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.