0

In my application I have varchar field in mysql for 365 days in particular format i.e. '0000-04-12' (12th of April). Right now I am having a function that should check if particular date from field falls in daylight saving zone of Calgary-Canada, which ranges from 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November. and returns true or false accordingly.

<?php 
/*
 @param $time form Database e.g. 0000-04-12
*/
function isDaylightSaving($time){
    // to replace 0000 in start with current year
    $today = substr_replace($time,date('Y'),0,4);
    $date = new DateTime($today);
    $week = $date->format('W');

    // TO DO 
    // find date falls between 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November.

    return ;
}

?>

Right now its returning the week with respect to particular year but I need to check week with respect to month to perform particular logic, which I am unable to get.Thanks.

entpnerd
  • 10,049
  • 8
  • 47
  • 68
Faisal Naseer
  • 4,110
  • 1
  • 37
  • 55
  • So, you're relying on the default time zone of your server? How is the date saved in MySQL? As UTC or is there an offset present? Also, please check [this question and answer](http://stackoverflow.com/questions/21946276/php-daylight-savings-conundrum). – Mjh Apr 12 '16 at 11:31
  • @Mjh we can see the DateTime init, so all DateTime will have the same TZ. Furthermore, he does not seem to have any TZ intel here, so we can assume than the DST is in appliance, depending on provided dates. – Lpu8er Apr 12 '16 at 11:39
  • @Mjh its not in utc its in string format like '0000-04-12' and im stuck at particular problem. – Faisal Naseer Apr 12 '16 at 11:39
  • Ok, so when you saved the date, what was the time zone used for creating such a string? The date/time had to be produced by some sort of software, what time zone did it use? You need to have a starting point and conversion point. You have the conversion point (`Canada/Calgary`). You need to find out the starting one so you can accurately determine whether DST is in effect for a particular date in your time zone. – Mjh Apr 12 '16 at 11:46
  • right now im just converting to simple time date $today = substr_replace($time,date('Y'),0,4); $date = new DateTime($today); but i have searched that calgary uses Mountain Time Zone UTC-07:00 – Faisal Naseer Apr 12 '16 at 12:07

2 Answers2

-1

Try this:

if($time > date('Y-m-d',strtotime('second sunday of march')) &&
   $time < date('Y-m-d',strtotime('first sunday of november'))){
    return true;
}else{
    return false;
}

strtotime() is a great function.

Ezenhis
  • 997
  • 9
  • 14
-1

The example given by @Ezenhis sounds good, but lacks of some elements. Extending it:

$low = new DateTime('@'.strtotime('second sunday of march'));
$high = new DateTime('@'.strtotime('first sunday of november'));
if($date > $low && $date < $high) {
    // we're good
}

You shouldn't compare strings from date functions. Using DateTime is also a better way to handle dates/times in PHP since PHP 5.0.

Lpu8er
  • 227
  • 1
  • 5