note 62399 deleted from function.mktime by bjori
| From: | bjori@php.net | Date: | Thu, 01 Jun 2006 18:38:56 +0000 |
| Subject: | note 62399 deleted from function.mktime by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-112809@lists.php.net to get a copy of this message | ||
Note Submitter: ajaypal at SPAMorNOSPAM dot ajaypal dot com
----
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;
}
?>