简体   繁体   English

如何从数字中获取 PHP 中的特定日期?

[英]How can I get a specific day of the month in PHP from a number?

Basically, I have a date set to today.基本上,我将日期设置为今天。 From that date, I want to get the next and previous 20th day of the month.从那个日期开始,我想获得该月的下一个和上一个 20 日。 Basically, starting with 07/10/2020 (date of the post in d/m/Y format), I should end up with a date/timestamp to 20/09/2020 (previous 20th) and 20/10/2020 (next 20th).基本上,从 07/10/2020(以 d/m/Y 格式发布的日期)开始,我应该以 20/09/2020(上一个 20 日)和 20/10/2020(下一个)结束第 20 条)。

I tried a few things and ended up with:我尝试了几件事,结果是:

$ld_next_str = date("d/m/Y", strtotime("20 day this month"));
$ld_next_dt = DateTime::createFromFormat("d/m/Y", $ld_next_str);

$ld_prev_str = date("d/m/Y", strtotime("20 day last month"));
$ld_prev_dt = DateTime::createFromFormat("d/m/Y", $ld_prev_str);

But turns out this : "20 day this month" just adds 20 days to the current day so right now (on the seventh) it returns the 27th.但事实证明: "20 day this month" 20 天"20 day this month"只是将 20 天添加到当前日期,因此现在(在第七天)它返回 27 天。

Knowing this, I figured I could probably just do something like subtracting the current day to that date (27 - 7 = 20) but I feel like there's probably a simpler way to do it, using strtotime without any extra steps.知道了这一点,我想我可能只是做一些事情,比如将当天减去那个日期 (27 - 7 = 20),但我觉得可能有一种更简单的方法,使用strtotime而不需要任何额外的步骤。

Thanks in advance!提前致谢!

PS : I know you can get some days using "first" / "second" / "third" etc... but (I think) it doesn't go any higher than "twelfth", because I tried "twentieth" and that didn't work... PS:我知道你可以使用“第一”/“第二”/“第三”等来获得一些日子......但是(我认为)它不会高于“第十二”,因为我尝试过“第二十”没用...

The formula for the previous 20th is:前 20 日的公式为:

mktime(0, 0, 0, $month - (1 - floor($day / 20)), 20, $year))

The formula for the next 20th is:下一个 20 日的公式是:

mktime(0, 0, 0, $month + floor($day / 20), 20, $year))

As a demo:作为演示:

for ($month = 1; $month <= 12; $month++) {
    for ($day = 1; $day <= 30; $day += 5) {
        $prev = mktime(0, 0, 0, $month - (1 - floor($day / 20)), 20, 2020);
        $next = mktime(0, 0, 0, $month + floor($day / 20), 20, 2020);
        echo "$day/$month/2020: ", date('d/m/Y', $prev), ', ', date('d/m/Y', $next), PHP_EOL;
    }
}

Outputs:输出:

1/1/2020: 20/12/2019, 20/01/2020
6/1/2020: 20/12/2019, 20/01/2020
11/1/2020: 20/12/2019, 20/01/2020
16/1/2020: 20/12/2019, 20/01/2020
21/1/2020: 20/01/2020, 20/02/2020
26/1/2020: 20/01/2020, 20/02/2020
1/2/2020: 20/01/2020, 20/02/2020
6/2/2020: 20/01/2020, 20/02/2020
11/2/2020: 20/01/2020, 20/02/2020
16/2/2020: 20/01/2020, 20/02/2020
21/2/2020: 20/02/2020, 20/03/2020
...

simply that:简单地说:

$lastM = date("20/m/Y", strtotime("-1 month"));
$thisM = date("20/m/Y", time());
$nextM = date("20/m/Y", strtotime("+1 month"));

echo "$lastM <br /> $thisM <br /> $nextM";

Solution with an external class (a DateTime extension) that supports the cron syntax.具有支持cron语法的外部类(DateTime 扩展)的解决方案。

The string "0 0 20 * *" specifies the 20th of every month and every weekday at 00:00.字符串“0 0 20 * *”指定每个月的 20 号和每个工作日的 00:00。 The nextCron() and previousCron() methods are used to determine the previous/next point in time based on a specific date. nextCron() 和 previousCron() 方法用于根据特定日期确定上一个/下一个时间点。

$cronStr = "0 0 20 * *";  //every 20th 00:00

//source on https://github.com/jspit-de/dt
$next20 = dt::create('now')->nextCron($cronStr);
echo $next20;  //2020-10-20 00:00:00

$prev20 = dt::create('now')->previousCron($cronStr);
echo $prev20;  //2020-09-20 00:00:00

Outputs for the execution on October 8, 2020. 2020 年 10 月 8 日执行的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM