note 79224 deleted from function.strtotime by danbrown

From: Date: Mon, 18 May 2009 02:22:50 +0000
Subject: note 79224 deleted from function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155649@lists.php.net to get a copy of this message
Note Submitter: jay at jaymunda dot com ---- I had a whole bunch of trouble with a calendar where I was trying to create recurring events. The problem was that for certain dates, the 'second friday' of the month would work and would not. There are a lot of flaws with the way I was processing, so I wrote this instead of using strtotime() to solve my issue. This solves the simple problem of finding the x weekday of any month. (first friday of 11-2007 or last tuesday of Dec, 1962) <?php function get_day( $describer, $weekday, $reference_date ) { //$reference_date format = m-Y $d = explode('-',$reference_date); switch ($describer) { case 'first': $offset = get_day_offset($reference_date, $weekday); break; case 'second': $offset = get_day_offset($reference_date, $weekday) + 7; break; case 'third': $offset = get_day_offset($reference_date, $weekday) + 14; break; case 'fourth': $offset = get_day_offset($reference_date, $weekday) + 21; break; case 'last': $reference_date = ($d[0]+1).'-'.($d[1]); $d = explode('-',$reference_date); $offset = get_day_offset($reference_date, $weekday) - 7; break; } $r = mktime( 0, 0, 0, $d[0], 1+$offset, $d[1] ); return $r; //returns timestamp format } function get_day_offset( $anchor , $target ) { //$anchor format = m-Y $ts = explode('-',$anchor); $ts = mktime(0,0,0,$ts[0],'01',$ts[1]); $anchor = date("w",$ts); $target = strtolower($target); $days = array( 'sunday'=>0, 'monday'=>1, 'tuesday'=>2, 'wednesday'=>3, 'thursday'=>4, 'friday'=>5, 'saturday'=>6 ); $offset = $days[$target] - $anchor; if ($offset<0) $offset+=7; return $offset; //returns 0-6 for use in get_day(); } $date1 = get_day("second", "saturday", "12-2007"); $date2 = get_day("last", "friday", "11-2007"); echo "Second Saturday of December, 2007: ".date("m-d-Y", $date1)."<br>"; echo "Last Friday of November, 2007: ".date("m-d-Y", $date2)."<br>"; ?> Outputs: Second Saturday of December, 2007: 12-08-2007 Last Friday of November, 2007: 11-30-2007

« previous php.notes (#155649) next »