php 如何使用PHP查找下个月的第一天和直到该日期的剩余天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1777384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:48:34  来源:igfitidea点击:

How to find first day of the next month and remaining days till this date with PHP

php

提问by Utku Dalmaz

How can I find first day of the next month and the remaining days till this day from the present day?

如何找到下个月的第一天以及从今天到今天的剩余天数?

Thank you

谢谢

回答by Benji XVI

Create a timestamp for 00:00 on the first day of next month:

创建下个月第一天 00:00 的时间戳:

$firstDayNextMonth = strtotime('first day of next month');

The number of days til that date is the number of seconds between now and then divided by (24 * 60 * 60).

到那个日期的天数是从现在到那个日期之间的秒数除以 (24 * 60 * 60)。

$daysTilNextMonth = ($firstDayNextMonth - time()) / (24 * 3600);

回答by Venkat Kotra

$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));

For getting first day after two months from current

从当前开始两个月后获得第一天

$firstDayAfterTwoMonths = date('Y-m-d', strtotime('first day of +2 month'));

回答by rydje

You can use DateTime object like this to find out the first day of next month like this:

您可以像这样使用 DateTime 对象来找出下个月的第一天,如下所示:

$date = new DateTime('first day of next month');

You can do this to know how many days left from now to the first day of next month:

您可以这样做以了解从现在到下个月的第一天还剩多少天:

$date = new DateTime('now');
$nowTimestamp = $date->getTimestamp();
$date->modify('first day of next month');
$firstDayOfNextMonthTimestamp = $date->getTimestamp();
echo ($firstDayOfNextMonthTimestamp - $nowTimestamp) / 86400;

回答by Yada

The easiest and quickest way is to use strtotime() which recognizes 'first day next month';

最简单快捷的方法是使用 strtotime() 来识别“下个月的第一天”;

$firstDayNextMonth = date('Y-m-d', strtotime('first day next month'));

回答by Matthew Hilgenfeld

Since I googled this and came to this answer, I figured I'd include a more modern answer that works for PHP 5.3.0+.

由于我在谷歌上搜索了这个并得出了这个答案,我想我会包含一个适用于 PHP 5.3.0+ 的更现代的答案。

//Your starting date as DateTime
$currentDate = new DateTime(date('Y-m-d'));

//Add 1 month
$currentDate->add(new DateInterval('P1M'));

//Get the first day of the next month as string
$firstDayOfNextMonth = $currentDate->format('Y-m-1');

回答by Alex

$firstDayNextMonth = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));

$firstDayNextMonth = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));

回答by mattbasta

You can get the first of the next month with this:

您可以通过以下方式获得下个月的第一天:

$now = getdate();
$nextmonth = ($now['mon'] + 1) % 13 + 1;
$year = $now['year'];
if($nextmonth == 1)
    $year++;
$thefirst = gmmktime(0, 0, 0, $nextmonth, $year);

With this example, $thefirstwill be the UNIX timestamp for the first of the next month. Use dateto format it to your liking.

在此示例中,$thefirst将是下个月第一天的 UNIX 时间戳。用于date根据您的喜好对其进行格式化。

This will give you the remaining days in the month:

这将为您提供该月的剩余天数:

$now = getdate();
$months = array(
    31,
    28 + ($now['year'] % 4 == 0 ? 1 : 0), // Support for leap years!
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
);
$days = $months[$now['mon'] - 1];
$daysleft = $days - $now['mday'];

The number of days left will be stored in $daysleft.

剩余天数将存储在$daysleft.

Hope this helps!

希望这可以帮助!

回答by Chad Lenartz

As another poster has mentioned the DateInterval object does not give accurate results for the next month when you use dates such as 1/31/2016 or 8/31/2016 as it skips the next month. My solution was to still use the DateInterval object but reformat your current date to be the first day of the current month prior to utilizing the DateInterval.

正如另一张海报所提到的,当您使用 1/31/2016 或 8/31/2016 等日期时,DateInterval 对象不会给出下个月的准确结果,因为它会跳过下个月。我的解决方案是仍然使用 DateInterval 对象,但在使用 DateInterval 之前将当前日期重新格式化为当月的第一天。

$currentDate = '8/31/2016';
$date = new DateTime(date("n", strtotime($currentDate))."/1/".date("Y", strtotime($currentDate)));
//add 1 month
$date->add(new DateInterval('P1M'));
$currentDate=$date->format('m/1/Y'); 

echo($currentDate);

回答by Sourav Sarkar

easiest way to get the last day of the month

获取本月最后一天的最简单方法

date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));

回答by user2505935

Even easier way to get first and last day of next month

获得下个月第一天和最后一天的更简单方法

$first = strttotime("first day of next month");
$last = strttotime("last day of next month");