I have the following function that is used to get the correct time and is used for my website. Unfortunately, It doesn't check for daylight savings time and is causing a time error. I was wondering if anyone would have a possible solution or help me out with my issue?

I am very new to PHP coding and am still getting my footing with it. It would be really helpful if someone would be able to help me with a way to have it automatically make the switch for in the future.

Here is the function:

function getDSTDifference() {
    $timezone = date_default_timezone_get();

    $newTZ = new DateTimeZone($timezone);
    $trans = $newTZ->getTransitions();

    $offset = $trans[0]['offset'] /60 /60;

    return $offeset;
}

---EDIT---

To better clarify what I am trying to say:

I have a website that monitors call times. These call times become off by an hour during the switch between DST and Regular Time (and vice versa). For example this past weekend caused times to show up like this: -57:01:23. Instead of have the function the way that it is - where I would have to manually go in and uncomment/comment out the two lines of code every time DST and Regular Time switch, is there a possible solution to making the function be able to do this automatically? Kinda like a more permanent solution. To me it just seems like redundant coding to have to constantly revisit that function to make what seems like a simple change over and over again. Again, I have not done a lot of work with PHP code before, therefore I am not familiar with built in functions that can be used or if I would have to create this on my own. If anybody would have some information/help to go about this, it would be much appreciated.

function getDSTDifference() {
    $timezone = date_default_timezone_get(); 

    $NewTZ = new DateTimeZone($timezone); 
    $transition = $NewTZ->getTransitions(); 

    $offset = $transition[0]['offset'] /60 /60; 

    //The following two lines need to be commented out when it is 
    //daylight savings time
    //They need to be uncommented when Daylight Savings Time ends

    $dst = $transition[0]['isdst']; 
    $offset = $offset - $dst;

    return $offset;
}

Hopefully that makes more sense! Thanks in advance for the help!

有帮助吗?

解决方案

One possibility to calculate the difference between two given hours is to use DateTime::diff(). At the time of writing this answer there was a PHP bug and you had to convert to UTC before:

<?php

$zone = new DateTimeZone('Europe/Madrid');
$start = new DateTime('2013-03-31 1:59:00 ', $zone);
$end = new DateTime('2013-03-31 3:00:00', $zone);

// Workaround for bug #63953
// No longer required since PHP/5.6.0, PHP/5.5.8 or PHP/5.4.24
$start->setTimeZone(new DateTimeZone('UTC'));
$end->setTimeZone(new DateTimeZone('UTC'));

$difference = $start->diff($end);
echo $difference->format('%H:%I:%S');

... prints 00:01:00 because that's when DST started in Western Europe.

You can also use Unix timestamps, which represent a fixed moment in time thus do not depend on time zones:

<?php

$zone = new DateTimeZone('Europe/Madrid');
$start = new DateTime('2013-03-31 1:59:00 ', $zone);
$end = new DateTime('2013-03-31 3:00:00', $zone);

$difference = $end->format('U') - $start->format('U');
echo "$difference seconds";

... prints 60 seconds.


Edit #1: What do you mean? My snippet contains sample data so you can test it—in real code you'll use your real data. You can (and should) set the correct time zone as default so you don't need to specify it every time. But even if you don't, the server's time zone will never change—even if you decide to physically move the computer to another state or country several times a year you can still opt for a fixed time zone of your choice (your app's time zone can be different from your server's).

If you really want a solution that requires you to change the code manually twice a year (for whatever the reason, maybe to charge maintenance fees), you'd better skip date/time functions and use strings; otherwise you'll risk PHP doing the calculations for you.

Edit #2:

  1. $start and $end represent your data. I chose sample data one minute before DST just to illustrate that the diff code works fine. It's the same as when you see <?php echo "Hello, World!"; ?> in a PHP tutorial: Hello, World! is sample data to illustrate how echo works but you don't have to use Hello, World! when you write your app.

  2. To convert from GMT to EST with PHP you create a date that belongs to GMT:

    $date = new DateTime('14:30', new DateTimeZone('GMT'));
    echo 'GMT: ' . $date->format('r') . PHP_EOL;
    // GMT: Thu, 07 Nov 2013 14:30:00 +0000
    

    ... and then switch to EST:

    $date->setTimeZone(new DateTimeZone('EST'));
    echo 'EST: ' . $date->format('r') . PHP_EOL;
    // EST: Thu, 07 Nov 2013 09:30:00 -0500
    

    However, if your original dates are in GMT, converting them to EST before substracting them does not provide any benefit.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top