第一个星期二PHP [英] First Tuesday PHP

查看:47
本文介绍了第一个星期二PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

first_tuesday()函数应返回一年中第一个星期二的日期,但尤其是在2011年的情况下,它返回错误的值。如何修复代码以使其在所有情况下均有效

The first_tuesday() function should return the date of the first Tuesday of year, but especially in the case of 2011 it returns a wrong value. how to Fix the code so it works in all cases

function first_tuesday($year){

    $first_january = mktime(0,0,0,1,1,$year);
    $day_week = date("w",$first_january );
    $first_tuesday = $first_jan + ((2 - $day_week) % 7)* 86400;
    return date("d/m/Y",$first_tuesday);
}


推荐答案

使用 DateTime 类:

function first_tuesday($year){
    $day = new DateTime(sprintf("First Tuesday of January %s", $year));
    return $day->format('d/m/Y');
}

用法:

echo first_tuesday(2011);

输出:

04/01/2011

这篇关于第一个星期二PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆