Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Tuesday PHP [duplicate]

Tags:

date

php

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);
}
like image 866
user3172273 Avatar asked Dec 16 '22 02:12

user3172273


1 Answers

strtotime is clever enough to get what you want:

function first_tuesday($year){
  return date('d/m/Y', strtotime("first Tuesday of January $year"));
}
like image 175
xdazz Avatar answered Dec 17 '22 16:12

xdazz