一个小问题来计算我今天写的从今天开始的剩余天数:
$today = date('Y/m/d');
$timestamp = strtotime($today);
$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
echo $daysRemaining;
剩下的日子我会得到
要进行测试,我输入了2月的静态日期
$timestamp = strtotime('2020-02-01');
$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
echo $daysRemaining;
这里的问题是,我如何计算考虑years年的月份的剩余天数,例如,到2020年2月将有29天,这样我就摆脱了剩下的28天
答案 0 :(得分:3)
停止使用函数并开始使用DateTime类!!! 该代码应自行说明。
<?php
$x = new DateTime('2020-02-17'); // create your date
$y = clone $x; // copy the date
$y->modify('last day of this month'); // alter the copy to the last day
echo $x->format('d') . "\n"; // show the day of the first date
echo $y->format('d') . "\n"; // show the day of the second date
echo $y->format('d') - $x->format('d'); // show the difference between the two
输出:
17
29
12
检查
处检查DateTime类文档