PHP日期时间文本表示法

PHP日期时间文本表示法,php,datetime,Php,Datetime,我想知道是否可以使用PHP DateTime将其作为输出: “2021年2月的第一个星期四” 另一个方向是 $date = new DateTime('first thursday of February 2021'); 我必须完成的事情:我有一个给定的日期,我想知道是否是第一个星期四,但不建立日期并比较之后的日期,比如: $date = new DateTime('first thursday of February 2021'); $given_date = new DateTime('2

我想知道是否可以使用PHP DateTime将其作为输出:

“2021年2月的第一个星期四”

另一个方向是

$date = new DateTime('first thursday of February 2021');
我必须完成的事情:我有一个给定的日期,我想知道是否是第一个星期四,但不建立日期并比较之后的日期,比如:

$date = new DateTime('first thursday of February 2021');
$given_date = new DateTime('2021-02-04');
return ($given_date == $date);
有人吗? 我忘了:我给定的日期可以是三月的第二个星期六或六月的第三个星期五,依此类推

我将为那些不理解我的问题的人进一步解释。想象一张时间表,上面写着:所有周四的办公室从早上8点到下午5点开放,但3月的第一个周四办公室将在早上7点开放,下午6点关闭。现在,一位客户想安排一次会议,我们只有一个日期,但必须知道:今天是不是第一个星期四。我的数据库保存诸如日期、月份以及月份中可能的位置等信息。我现在的解决方案是一个循环来确定月份的位置

private function getPositionInMonth(DateTime $date)
{
    $positions = ['first', 'second', 'third', 'fourth', 'fifth'];
    $day_name = $date->format('l');
    foreach ($positions as $i => $position) {
        $str = $position . ' ' . $day_name . ' ' . $date->format('F') . ' ' . $date->format('Y');
        $test_date = new DateTime($str);
        if ($test_date->format('d.m.Y') == $date->format('d.m.Y')) {
            return ($i + 1);
        }
    }
}

可以在sql查询中获取所有其他信息。

日期时间对象的格式应为字符串,如“2021年2月的第一个星期四”


DateTime对象支持相对格式。为此,您可以调用方法
modify

<?php
$date = new DateTime('now');
$date->modify('first thursday of February 2021');
$given_date = new DateTime('2021-02-04');

return ($given_date == $date);

像CBroe建议的那样,用一点点数学来解决它:

private function getPositionInMonth(DateTime $date)
{
    $test_date = new DateTime($date->format('Y-m-') . '01');
    $diff = round(($date->getTimestamp() -  $test_date->getTimestamp()) / 86400, 0);
    if($diff < 7) {
        return 1;
    }
    return (int)($diff / 7) + 1;
}
私有函数getPositionInMonth(DateTime$date)
{
$test_date=new DateTime($date->format('Y-m-')。'01');
$diff=round($date->getTimestamp()-$test\u date->getTimestamp())/86400,0);
如果($diff<7){
返回1;
}
返回值(整数)($diff/7)+1;
}
该函数将在一个月内给出该职位

private function getPositionInMonth(DateTime $date)
{
    $positions = ['first', 'second', 'third', 'fourth', 'fifth'];
    $day_name = $date->format('l');
    foreach ($positions as $i => $position) {
        $str = $position . ' ' . $day_name . ' ' . $date->format('F') . ' ' . $date->format('Y');
        $test_date = new DateTime($str);
        if ($test_date->format('d.m.Y') == $date->format('d.m.Y')) {
            return ($i + 1);
        }
    }
}

(Diff calculation fixed,DateTime给出了3月29日的d=0和m=1,现在它可以工作了。如果您发现任何日期失败,请注明)

另一种方法:不,我认为没有自定义代码就无法完成。
格式
方法不包含这样的逻辑。太糟糕了,这样会很舒服。不,这实际上是非常荒谬的。为什么您的脚本逻辑要确定某个日期是否是当月第一个(、第二个、第三个……)发生的日期,而要依赖于比较该事实在特定语言中的文本表示?这应该用一点数学来解决。他说,你是对的。寻找文本表示是胡说八道,而且我认为日期时间不正确。用2021-05-08测试,这是5月的第二个星期六,但DateTime给出了第一个星期六。问题是是否有一种内置的方式从
DateTime
对象输出2021年2月的第一个星期四。我误解了这个问题。新的试验。看我的问题,我添加了一些解释。如果有一个内置函数返回给定日期2021-03-10的“2021年3月第二个星期三”,那就太好了。您误解了这个问题。它提出的是相反的问题-从值创建日期,例如
2021-02-04 00:00:00
,然后让对象打印解释,例如
2021年2月的第一个星期四
。请为您的答案添加一些解释,以便其他人可以从中学习您认为现在更好吗?请检查getPositionInMonth(新日期时间('2021-03-29'));对于时区“欧洲/柏林”,我得到1作为结果。