First Tuesday PHP

date, php

Solution

Use `DateTime` class instead:

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

Usage:

echo first_tuesday(2011);

Output:

04/01/2011

Problem

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); } ```

Original source

Related problems