PHP strtotime() “二月第一个星期一"如果 2 月 1 日是星期一,则返回第二个星期一

2024-04-13php开发问题
5

本文介绍了PHP strtotime() “二月第一个星期一"如果 2 月 1 日是星期一,则返回第二个星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个计算假期的 PHP 函数:

I'm working on a PHP function which calculates holidays:

function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)

假期以数组中的时间戳形式返回.

The holidays are returned as timestamps in an array.

因为我必须计算像二月的第一个星期一这样的日期,所以我尝试了 strtotime("first monday february $year"),我发现这对于 2010 年不起作用,因为 02/01/2010 是星期一 - 我改为 2 月 8 日.

Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year") and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.

这个bug其实是在变更日志中提到的:在 5.2.7 之前的 PHP 5 中,如果该工作日是该月的第一天,则请求在一个月内出现给定的工作日会错误地将一周添加到返回的时间戳.这已在 5.2.7 及更高版本中得到纠正.

This bug is actually mentioned in the change log: In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

但我使用的是 PHP 5.3.8.为什么我会遇到此错误?

But I'm using PHP 5.3.8. Why am I experiencing this error?

推荐答案

看起来你只是少了一个of":

Looks like you are just missing an "of":

echo date('Y-m-d', strtotime('first monday of february 2010'));

将给出预期结果.有关各种输入格式,请参阅 PHP 相对日期手册.

will give the expected result. See the PHP Manual on Relative dates for the various input formats.

这篇关于PHP strtotime() “二月第一个星期一"如果 2 月 1 日是星期一,则返回第二个星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17