note 62399 added to function.mktime

From: Date: Mon, 27 Feb 2006 21:00:50 +0000
Subject: note 62399 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-104512@lists.php.net to get a copy of this message
Here is a small PHP function that will return timestamp of any given weekday just before the given timestamp of a date. My use for writing this function was to find weekday Monday w.r.t. to any given date. e.g date on which Monday was there just before a given date. As usual lot of scope for optimisation. <? /** If no argument are provided, default to moday from current date Should work with daylightsavings etc. $weekday: is the day you are looking for: Sun: 0, Mon: 1, Tue: 3 ... etc. $givenDateTimestamp: is the timestamp of date just before which you are looking for that weekday, use mktime to convert a date to timestamp return value: timestamp of week day just before the given date. Use date() to conver back to any date format. */ function getTimestampOfPrevWeekDayFromGivenDate ($weekdaytofind = 1, $givenDateTimestamp = 0) { if(!$givenDateTimestamp) { //Build Today $tmonth=date('n'); $tday=date('d'); $tyear=date('Y'); } else { $tmonth=date('n', $ts); $tday=date('d', $ts); $tyear=date('Y', $ts); } //convert it to unix timestamp, just in case it may be invalid, //possible scope for optimisation $ts = mktime(0, 0, 0,$tmonth,$tday,$tyear); for($weekday=date("w", $ts); $weekday != $weekdaytofind; $weekday=date("w", $ts)) { //easy way to adjust, posibility of optimisation $tmonth=date('n', $ts); $tday=date('d', $ts); $tyear=date('Y', $ts); //convert it to unix timestamp $ts = mktime(0, 0, 0, date("m") , $tday-1, date("Y")); } return $ts; } ?> ---- Server IP: 203.199.124.111 Probable Submitter: 203.134.194.200 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/note/edit/62399 Del: integrated -- http://master.php.net/note/delete/62399/integrated Del: useless -- http://master.php.net/note/delete/62399/useless Del: bad code -- http://master.php.net/note/delete/62399/bad+code Del: spam -- http://master.php.net/note/delete/62399/spam Del: non-english -- http://master.php.net/note/delete/62399/non-english Del: in docs -- http://master.php.net/note/delete/62399/in+docs Del: other reasons-- http://master.php.net/note/delete/62399 Reject -- http://master.php.net/note/reject/62399 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104512) next »