php 中的开始日期和结束日期差异

发布于 2024-10-05 14:39:58 字数 79 浏览 11 评论 0原文

我将要求用户输入开始日期和结束日期。我想获取从开始到结束的所有日期,并获取这些日期之间的天数。给定开始和结束日期,如何在 PHP 中执行此操作。

I will ask the user to enter start date and end date. I would want to get all of the dates from start to end and get the number of days between those dates. How can I do this in PHP given start and end date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↙温凉少女 2024-10-12 14:39:58

处理日期时,您可以按照 Ignacio 的建议转换为时间戳(秒)。但一般来说,如果可能的话,您最好使用实际日期和日期。日子更上一层楼。

为此,请参阅 PHP 中内置的 DateTime 类:

http://www.php。 net/manual/en/class.datetime.php

http:// www.php.net/manual/en/class.dateinterval.php

这些在 PHP 5.2 中得到了很好的支持,但 PHP 5.3 添加了更好的 DateTime 处理功能。

When dealing with dates, you can convert to timestamps (seconds) as Ignacio recommends. But generally if possible you will be better off working with actual dates & days at a higher level.

For this, see the DateTime class built into PHP:

http://www.php.net/manual/en/class.datetime.php

http://www.php.net/manual/en/class.dateinterval.php

These are well supported in PHP 5.2, but PHP 5.3 adds even better DateTime handling functionality.

娇纵 2024-10-12 14:39:58

此代码的最终结果将是天数。

$days = (strtotime(日期("Ymd"))-strtotime("2010-08-20")) / (60 * 60 * 24);
回声$天;

End result of this code will be number of days.

$days = (strtotime(date("Y-m-d"))-strtotime("2010-08-20")) / (60 * 60 * 24);
echo $days;

零度° 2024-10-12 14:39:58

阅读我对此主题的回答,并举例说明:

在 PHP 中计算 2 个日期之间的小时数

Read my answer to this topic, with examples:

Calculate number of hours between 2 dates in PHP

故事还在继续 2024-10-12 14:39:58

根据用户提供的输入,我很可能会使用类似

$dateOne = (int)(mktime(0, 0, 0, $month1, $day1, $year1)/86400); //Get the first date as a unix timestamp, then convert to days.
$dateTwo = (int)(mktime(0, 0, 0, $month2, $day2, $year2)/86400); //Get the second date as a unix timestamp, then convert to days.

// Example
//$dateOne = (int)(mktime(0,0,0,12,03,2009)/86400);
//$dateTwo = (int)(mktime(0,0,0,08,19,2011)/86400);

$daysBetween = $dateTwo-$dateOne; //Calculate days between.

echo $daysBetween.'<br />'; //Echo the days between.

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.
    echo date('Y-m-d', $dateOne*86400+($i*86400)).'<br />';
}

上面的代码将为您提供所有日期,包括第一个和最后一个日期。要仅获取介于两者之间的更改:

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.

至:

for ($i=2; $i<=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.

已测试,效果良好。

注意:第一个日期必须早于最后一个日期。

干杯
查理

Depending on the input the user gives, I would most likely be using something like

$dateOne = (int)(mktime(0, 0, 0, $month1, $day1, $year1)/86400); //Get the first date as a unix timestamp, then convert to days.
$dateTwo = (int)(mktime(0, 0, 0, $month2, $day2, $year2)/86400); //Get the second date as a unix timestamp, then convert to days.

// Example
//$dateOne = (int)(mktime(0,0,0,12,03,2009)/86400);
//$dateTwo = (int)(mktime(0,0,0,08,19,2011)/86400);

$daysBetween = $dateTwo-$dateOne; //Calculate days between.

echo $daysBetween.'<br />'; //Echo the days between.

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.
    echo date('Y-m-d', $dateOne*86400+($i*86400)).'<br />';
}

The above code will give you all the days, including the first and last date. To get only the ones in between change:

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.

to:

for ($i=2; $i<=$daysBetween; $i++) { //Loop through every day between, echo the date of that day.

Have tested, works well.

NOTE: the first date MUST be before the last date.

Cheers
Charlie

我也只是我 2024-10-12 14:39:58

将两个日期都转换为时间戳,然后从一个日期到另一个日期,再转换回日期。

Convert both dates to timestamps, then count from one to the other, converting back to dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文