note 29907 added to function.date
| From: | leafrink at hotmail dot com | Date: | Fri, 28 Feb 2003 06:26:20 +0000 |
| Subject: | note 29907 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44649@lists.php.net to get a copy of this message | ||
Wrote this function after a fair bit of searching on the subject.
Don't know if it's the best way to do it, but it works.
Its not that exact but it's pretty close.
Hope it helps someone out there.
/**
* function for working out if it is daylight savings for a specific date.
* must specify when daylight savings starts and ends in the format
* "[1=First,2=Second,3=Third,4=Fourth,L=Last]:NameOfDay:Of The Month"
* e.g. "L:Sun:3" = Last Sunday in March
* some will not meet this criteria but most do.
*
* see - http://webexhibits.org/daylightsaving/g.html
for daylight savings times around the world.
*
*/
function is_daylight_savings($gmtime,$DSTStart = '',$DSTEnd = ''){
//global $locale;
//For Most Of Australia
//DayLightSavings Starts Last sunday in October
$DSTStart = "L:Sun:10";//$locale['DSTStart'];
//DayLightSavings Ends Last Sunday in March
$DSTEnd = "L:Sun:3";//$locale['DSTEnd'];
$DSTStart = split(":",$DSTStart);
$DSTEnd = split(":",$DSTEnd);
$gmtMonth = date("n",$gmtime);
// if not even in the Important changeover months.
if ($gmtMonth < $DSTStart[2] && $gmtMonth > $DSTEnd[2]){
return false;
} else if ($gmtMonth > $DSTStart[2] || $gmtMonth < $DSTEnd[2]) {
return true;
} else {
//it is in the Start or End Month
if ($gmtMonth == $DSTStart[2]){
$True = true;
$week = $DSTStart[0];
$ImportantDay = $DSTStart[1];
} else {//it is in the End Month
$True = false;
$week = $DSTEnd[0];
$ImportantDay = $DSTEnd[1];
}
//get the day of the month
$gmtDay = date("j",$gmtime);
//work out what week it starts/ends.
if ($week == 'L'){
$week = 4;
$ldom = 4;//last day of month factor
}
//if the week in which it starts/ends has not been reached
if($gmtDay < ($week-1)*7){
return (!$True);
} else {
$gmtDate = getdate($gmtime);
//go by a Day of the Week Basis
for ($i=($week-1)*7;$i<(($week*7)+$ldom);$i++){
$checkDate = mktime(0,0,0,$gmtDate["mon"],$i,$gmtDate["year"]);
//get the actual day it starts/ends
if (date("D",$checkDate) == "Sun" &&
date("n",$checkDate) == $gmtMonth ){
$day = date("j",$checkDate);
}
}
if ($gmtDay < $day) {//if it has not reached the day
return (!$True);
} else {
return $True;
}
}
}
}
//Testing
if(is_daylight_savings(strtotime("26-Oct-2003"))){
echo "is DAYLIGHT SAVINGS";
}else {
echo "is NOT DAYLIGHT SAVINGS";
}
Regards :-)
--
http://www.php.net/manual/en/function.date.php
http://master.php.net/manage/user-notes.php?action=edit+29907
http://master.php.net/manage/user-notes.php?action=delete+29907
http://master.php.net/manage/user-notes.php?action=reject+29907