We use cookies to keep our site relevant and easy to use, your continued use of this site is consent that we may set several cookies (see our Privacy & Cookie Policy), click to always allow cookies from our site (and not see this notifcation on your next visit) or read more.Allow Cookies

EU legislation requires that all websites clearly specify if cookies are being used and their purpose, You can read more about how we use cookies (and which cookies we use) in our Privacy and Cookie Policy.

You will see this notification the first time you visit our website unless you accept cookies (in which case we'll set a cookie to remember thay you're happy for us to to set cookies!).

PHP Date/Time Check with BST

function BSTCheck() {
$ThisYear = (date("Y"));
$MarStartDate = ($ThisYear."-03-25");
$OctStartDate = ($ThisYear."-10-25");
$MarEndDate = ($ThisYear."-03-31");
$OctEndDate = ($ThisYear."-10-31");

//work out the Unix timestamp for 1:00am GMT on the last Sunday of March, when BST starts
while ($MarStartDate <= $MarEndDate) {
$day = date("l", strtotime($MarStartDate));
if ($day == "Sunday")
$BSTStartDate = ($MarStartDate);
$MarStartDate++;
}
$BSTStartDate = (date("U", strtotime($BSTStartDate))+(60*60));

//work out the Unix timestamp for 1:00am GMT on the last Sunday of October, when BST ends
while ($OctStartDate <= $OctEndDate) {
$day = date("l", strtotime($OctStartDate));
if ($day == "Sunday")
$BSTEndDate = ($OctStartDate);
$OctStartDate++;
}
$BSTEndDate = (date("U", strtotime($BSTEndDate))+(60*60));

//Check to see if we are now in BST
$now = mktime();
return (($now >= $BSTStartDate) && ($now <= $BSTEndDate)) ? TRUE : FALSE;
}

$DST = (BSTCheck()) ? 3600 : 0;
$time = gmdate("H:i", time() + $DST);
$night = ((date("H")>=17) OR (date("H")<5)) ? TRUE : FALSE;

This entry was posted on Friday, June 3rd, 2005 at 9:32 am and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.