gpt4 book ai didi

php - 计算一个月的剩余天数

转载 作者:可可西里 更新时间:2023-10-31 23:55:45 25 4
gpt4 key购买 nike

假设我有一个用户可以选择一个月中的某个日期。比方说,如果他选择 2014 年 10 月 16 日,我想将该月的剩余天数显示为日历。

<?php
error_reporting(0);

$data = $_POST['input'];
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');

$m = date('m'); $y = date('y');
$d = cal_days_in_month(CAL_GREGORIAN,$m,$y);

for ($i=1;$i<;$i++){
echo $i + 1;
}

到目前为止,代码非常困惑。我只是没有办法绕过这个,这就是我问的原因。

最佳答案

您可以使用 strtotimedate .

对于 date 的格式,您可以使用以下格式:

't' Number of days in the given month (28 through 31)
'j' Day of the month without leading zeros (1 to 31)

<?php

$timestamp = strtotime('2014-10-03');

$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);

var_dump($daysRemaining); // int(28)

DEMO


编辑:显然您想列出该月的剩余天数:

<?php

$timestamp = strtotime('2014-10-03');
$yearMonth = date('Y-m-', $timestamp);

$daysInMonth = (int)date('t', $timestamp);

for ($i = (int)date('j', $timestamp); $i <= $daysInMonth; $i++) {
$dateString = date('l \t\h\e jS \o\f F', strtotime($yearMonth . $i));

var_dump($dateString);
}

/*
string(25) "Friday the 3rd of October"
string(27) "Saturday the 4th of October"
string(25) "Sunday the 5th of October"
string(25) "Monday the 6th of October"
string(26) "Tuesday the 7th of October"
string(28) "Wednesday the 8th of October"
string(27) "Thursday the 9th of October"
string(26) "Friday the 10th of October"
string(28) "Saturday the 11th of October"
string(26) "Sunday the 12th of October"
string(26) "Monday the 13th of October"
string(27) "Tuesday the 14th of October"
string(29) "Wednesday the 15th of October"
string(28) "Thursday the 16th of October"
string(26) "Friday the 17th of October"
string(28) "Saturday the 18th of October"
string(26) "Sunday the 19th of October"
string(26) "Monday the 20th of October"
string(27) "Tuesday the 21st of October"
string(29) "Wednesday the 22nd of October"
string(28) "Thursday the 23rd of October"
string(26) "Friday the 24th of October"
string(28) "Saturday the 25th of October"
string(26) "Sunday the 26th of October"
string(26) "Monday the 27th of October"
string(27) "Tuesday the 28th of October"
string(29) "Wednesday the 29th of October"
string(28) "Thursday the 30th of October"
string(28) "Friday the 31st of October"
*/

DEMO

关于php - 计算一个月的剩余天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com