note 81962 modified in function.mktime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:29:38 +0000 |
| Subject: | note 81962 modified in function.mktime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142066@lists.php.net to get a copy of this message | ||
Another useful function that I have created years ago. Useful for all calendar applications, if you
have to switch to a date given by a week number and a specific day (e.g. tuesday in week 12:
mktimefromcw(2008, 12, 2)).
<?php
/**
* @return timestamp
* @param integer year
* @param integer woy
* @param integer dow
* @desc This function retrieves the time for the given year, week of year and day of the week and
returns it.
*/
function mktimefromcw($year, $woy=1, $dow=1) {
# $year = year (four digits)
# $woy = week of the year (1..53)
# $dow = day of the week (1..7)
$dow = ($dow) % 7;
$woy = ($woy) - 1;
# Get reference value (this is the first monday of the first week of the year, not easy to
calculate)
$fdoy_timestamp = mktime(0,0,0,1,1,$year);
$fdoy = ((date("w", $fdoy_timestamp) + 6) % 7) + 1;
if($fdoy == 1) {
# This first day of the year is a monday
$fcwstart = $fdoy_timestamp;
}
elseif($fdoy < 5) {
# The first day if before Friday, therefor the first Monday can be found in the previos year
(this is no fun, believe in it!).
$fcwstart = strtotime("last Monday", $fdoy_timestamp);
}
else {
# The first day is a friday or later, so the first days belong to calender week 53 (yes, this is
possible!) of the previous year, do not count them for this year.
$fcwstart = strtotime("next Monday", $fdoy_timestamp);
}
# Create timestamp
$timestr = date("d F Y", $fcwstart)." +$woy week +$dow day";
$time = strtotime($timestr);
# Return timestamp
return $time;
}
?>
--was--
Another useful function that I have created years ago. Useful for all calendar applications, if you
have to switch to a date given by a week number and a specific day (e.g. tuesday in week 12:
mktimefromcw(2008, 12, 2)).
/**
* @return timestamp
* @param integer year
* @param integer woy
* @param integer dow
* @desc This function retrieves the time for the given year, week of year and day of the week and
returns it.
*/
function mktimefromcw($year, $woy=1, $dow=1) {
# $year = year (four digits)
# $woy = week of the year (1..53)
# $dow = day of the week (1..7)
$dow = ($dow) % 7;
$woy = ($woy) - 1;
# Get reference value (this is the first monday of the first week of the year, not easy to
calculate)
$fdoy_timestamp = mktime(0,0,0,1,1,$year);
$fdoy = ((date("w", $fdoy_timestamp) + 6) % 7) + 1;
if($fdoy == 1) {
# This first day of the year is a monday
$fcwstart = $fdoy_timestamp;
}
elseif($fdoy < 5) {
# The first day if before Friday, therefor the first Monday can be found in the previos year
(this is no fun, believe in it!).
$fcwstart = strtotime("last Monday", $fdoy_timestamp);
}
else {
# The first day is a friday or later, so the first days belong to calender week 53 (yes, this is
possible!) of the previous year, do not count them for this year.
$fcwstart = strtotime("next Monday", $fdoy_timestamp);
}
# Create timestamp
$timestr = date("d F Y", $fcwstart)." +$woy week +$dow day";
$time = strtotime($timestr);
# Return timestamp
return $time;
}
http://php.net/manual/en/function.mktime.php