4

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);
}
Pascamel
  • 953
  • 6
  • 18
  • 28

5 Answers5

5

strtotime is clever enough to get what you want:

function first_tuesday($year){
  return date('d/m/Y', strtotime("first Tuesday of January $year"));
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
4

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
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    You probably meant `return` instead of `echo` in your function. :) – rink.attendant.6 Jan 08 '14 at 08:17
  • @rink.attendant.6: Right. Fixed it, thanks! – Amal Murali Jan 08 '14 at 08:17
  • by the way check this code $exclus=array(1,4); function hasard(){ $resultat=rand(1,10); return $resultat; } echo "Nombre au hasard : ".hasard()."\n"; – user3172273 Jan 08 '14 at 08:20
  • 2
    @user3172273: That's a different question. Please post it as such. Click on the [Ask question](http://stackoverflow.com/questions/ask) button. Make sure you include ALL the relevant details, and include the expected output in your question. Also, most of the users on this site don't speak , so I suggest you post the question (and code) in English. Cheers! – Amal Murali Jan 08 '14 at 08:23
3

This seems to work for me:

<?php

$first_tuesday = new DateTime("first Tuesday of January 2011");
echo $first_tuesday->format('Y-m-d H:i:s');

?>

Putting it into functional form:

<?php

/**
 * @return DateTime Returns a DateTime object representing the
 *                  first Tuesday of the year
 */
function first_tuesday($year){
    $first_tuesday = new DateTime("first Tuesday of January $year");
    return $first_tuesday;
}

?>

If you just want the string in your format, then:

<?php

/**
 * @return DateTime Returns a string representing the first
 *                  Tuesday of the year in the d/m/Y format
 */
function first_tuesday($year){
    $first_tuesday = new DateTime("first Tuesday of January $year");
    return $first_tuesday->format('d/m/Y');
}

?>

You can read more about the DateTime class in the PHP manual (which is also available in French, as that seems to be your native language based on the original question.)

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0
$start_of_year = strtotime("01.01.2011");
$first_tuesday = strtotime("tuesday", $start_of_year);

echo date('d.m.Y', $first_tuesday); // 04.01.2011
Victor Stanciu
  • 12,037
  • 3
  • 26
  • 34
0

Simple:

function first_tuesday($year)
{
    return date("d/m/Y",strtotime('first Tuesday january '.$year));
}

No need to use Objects, this is the fastest way of getting it.

Ervin
  • 2,374
  • 4
  • 30
  • 44