PHP - strtotime, specify timezone












48














I have a date string, say '2008-09-11'. I want to get a timestamp out of this, but I need to specify a timezone dynamically (rather then PHP default).



So to recap, I have two strings:



$dateStr = '2008-09-11';
$timezone = 'Americas/New_York';


How do I get the timestamp for this?



EDIT: The time of day will be the midnight of that day.... $dateStr = '2008-09-11 00:00:00';










share|improve this question




















  • 1




    Why taking the time zone into account when there is no time specified?
    – Gumbo
    Aug 25 '10 at 18:22










  • Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
    – Andy Hin
    Aug 25 '10 at 18:23










  • If you take midnight as the default hour it matters...
    – vlad b.
    Aug 25 '10 at 18:23










  • vlad b. What do you mean?
    – Andy Hin
    Aug 25 '10 at 18:25






  • 2




    Your timezone is invalid, it should be America/New_York
    – salathe
    Aug 25 '10 at 18:31
















48














I have a date string, say '2008-09-11'. I want to get a timestamp out of this, but I need to specify a timezone dynamically (rather then PHP default).



So to recap, I have two strings:



$dateStr = '2008-09-11';
$timezone = 'Americas/New_York';


How do I get the timestamp for this?



EDIT: The time of day will be the midnight of that day.... $dateStr = '2008-09-11 00:00:00';










share|improve this question




















  • 1




    Why taking the time zone into account when there is no time specified?
    – Gumbo
    Aug 25 '10 at 18:22










  • Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
    – Andy Hin
    Aug 25 '10 at 18:23










  • If you take midnight as the default hour it matters...
    – vlad b.
    Aug 25 '10 at 18:23










  • vlad b. What do you mean?
    – Andy Hin
    Aug 25 '10 at 18:25






  • 2




    Your timezone is invalid, it should be America/New_York
    – salathe
    Aug 25 '10 at 18:31














48












48








48


8





I have a date string, say '2008-09-11'. I want to get a timestamp out of this, but I need to specify a timezone dynamically (rather then PHP default).



So to recap, I have two strings:



$dateStr = '2008-09-11';
$timezone = 'Americas/New_York';


How do I get the timestamp for this?



EDIT: The time of day will be the midnight of that day.... $dateStr = '2008-09-11 00:00:00';










share|improve this question















I have a date string, say '2008-09-11'. I want to get a timestamp out of this, but I need to specify a timezone dynamically (rather then PHP default).



So to recap, I have two strings:



$dateStr = '2008-09-11';
$timezone = 'Americas/New_York';


How do I get the timestamp for this?



EDIT: The time of day will be the midnight of that day.... $dateStr = '2008-09-11 00:00:00';







php date timezone






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 25 '10 at 18:24

























asked Aug 25 '10 at 18:21









Andy Hin

10.4k3284127




10.4k3284127








  • 1




    Why taking the time zone into account when there is no time specified?
    – Gumbo
    Aug 25 '10 at 18:22










  • Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
    – Andy Hin
    Aug 25 '10 at 18:23










  • If you take midnight as the default hour it matters...
    – vlad b.
    Aug 25 '10 at 18:23










  • vlad b. What do you mean?
    – Andy Hin
    Aug 25 '10 at 18:25






  • 2




    Your timezone is invalid, it should be America/New_York
    – salathe
    Aug 25 '10 at 18:31














  • 1




    Why taking the time zone into account when there is no time specified?
    – Gumbo
    Aug 25 '10 at 18:22










  • Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
    – Andy Hin
    Aug 25 '10 at 18:23










  • If you take midnight as the default hour it matters...
    – vlad b.
    Aug 25 '10 at 18:23










  • vlad b. What do you mean?
    – Andy Hin
    Aug 25 '10 at 18:25






  • 2




    Your timezone is invalid, it should be America/New_York
    – salathe
    Aug 25 '10 at 18:31








1




1




Why taking the time zone into account when there is no time specified?
– Gumbo
Aug 25 '10 at 18:22




Why taking the time zone into account when there is no time specified?
– Gumbo
Aug 25 '10 at 18:22












Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
– Andy Hin
Aug 25 '10 at 18:23




Sorry for the confusion. It will be the midnight of that day (beginning of the specified day).
– Andy Hin
Aug 25 '10 at 18:23












If you take midnight as the default hour it matters...
– vlad b.
Aug 25 '10 at 18:23




If you take midnight as the default hour it matters...
– vlad b.
Aug 25 '10 at 18:23












vlad b. What do you mean?
– Andy Hin
Aug 25 '10 at 18:25




vlad b. What do you mean?
– Andy Hin
Aug 25 '10 at 18:25




2




2




Your timezone is invalid, it should be America/New_York
– salathe
Aug 25 '10 at 18:31




Your timezone is invalid, it should be America/New_York
– salathe
Aug 25 '10 at 18:31












6 Answers
6






active

oldest

votes


















69














$date = new DateTime($dateStr, new DateTimeZone($timezone));


$timestamp = $date->format('U');






share|improve this answer



















  • 1




    Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
    – Andy Hin
    Aug 25 '10 at 18:26








  • 4




    @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
    – salathe
    Aug 25 '10 at 18:29










  • I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
    – Andy Hin
    Aug 25 '10 at 18:38








  • 7




    @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
    – Artefacto
    Aug 25 '10 at 18:51












  • @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
    – Andy Hin
    Aug 25 '10 at 19:05



















21














The accepted answer is great if you're running PHP > 5.2 (I think that's the version they added the DateTime class). If you want to support an older version, you don't want to type as much, or if you just prefer the functional approach there is another way which also does not modify global settings:



$dateStr = '2008-09-11 00:00:00';
$timezone = 'America/New_York';
$dtUtcDate = strtotime($dateStr. ' '. $timezone);





share|improve this answer























  • I don't see how the accepted answer modifies global settings.
    – Sebastian Mach
    Aug 26 '15 at 11:54










  • It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
    – krowe
    Aug 26 '15 at 13:26





















16














This will work if for some reason you're using <5.2 (Heaven forbid).



$reset = date_default_timezone_get();
date_default_timezone_set('America/New_York');
$stamp = strtotime($dateStr);
date_default_timezone_set($reset);


But anything 5.2 and above, I'd strongly recommend you opt for @salathe's answer.






share|improve this answer























  • works but dont forget to change it back! i think salathes solution is better
    – The Surrican
    Aug 25 '10 at 18:28










  • What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
    – Jonah
    Aug 25 '10 at 18:33










  • This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
    – Dan Heberden
    Aug 25 '10 at 19:26










  • Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
    – Fr0zenFyr
    Mar 15 '18 at 10:22



















3














If you're going to use Timezones, I propose you use the DateTime class, and in this case the DateTime::createFromFormat() function which will allow you to do semothing like this:



$start = "2015-01-14 11:59:43";
$timezone = "America/Montreal";

$tz = new DateTimeZone($timezone);
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $start, $tz);


When you put $tz in the DateTime::createFromFormat function, you tell it what time zone the date you gave is in, so that when you need to convert it to another timezone, all you have to do is something like this:



$start = $dt->setTimeZone(new DateTimeZone('UTC'));





share|improve this answer





























    2














    Whenever you are referring to an exact moment in time, persist the time according to a unified standard that is not affected by daylight savings. (GMT and UTC are equivalent with this regard, but it is preferred to use the term UTC. Notice that UTC is also known as Zulu or Z time.)



    If instead you choose to persist a time using a local time value, include the local time offset from UTC, such that the timestamp can later be interpreted unambiguously.



    In some cases, you may need to store both the UTC time and the equivalent local time. Often this is done with two separate fields, but some platforms support a datetimeoffset type that can store both in a single field.



    When storing timestamps as a numeric value, use Unix time - which is the number of whole seconds since 1970-01-01T00:00:00Z (excluding leap seconds). If you require higher precision, use milliseconds instead. This value should always be based on UTC, without any time zone adjustment.



    If you might later need to modify the timestamp, include the original time zone ID so you can determine if the offset may have changed from the original value recorded.



    When scheduling future events, usually local time is preferred instead of UTC, as it is common for the offset to change. See answer, and blog post.



    Remember that time zone offsets are not always an integer number of hours (for example, Indian Standard Time is UTC+05:30, and Nepal uses UTC+05:45).



    If using Java, use java.time for Java 8, or use Joda Time for Java 7 or lower.
    If using .NET, consider using Noda Time.
    If using .NET without Noda Time, consider that DateTimeOffset is often a better choice than DateTime.
    If using Perl, use DateTime.
    If using Python, use pytz or dateutil.
    If using JavaScript, use moment.js with the moment-timezone extension.
    If using PHP > 5.2, use the native time zones conversions provided by DateTime, and DateTimeZone classes. Be careful when using.



    DateTimeZone::listAbbreviations() - see answer. To keep PHP with up to date Olson data, install periodically the timezonedb PECL package; see answer.



    If using C++, be sure to use a library that uses the properly implements the IANA timezone database. These include cctz, ICU, and Howard Hinnant's "tz" library.



    Do not use Boost for time zone conversions. While its API claims to support standard IANA (aka "zoneinfo") identifiers, it crudely maps them to fixed offsets without considering the rich history of changes each zone may have had.



    (Also, the file has fallen out of maintenance.)



    Most business rules use civil time, rather than UTC or GMT. Therefore, plan to convert UTC timestamps to a local time zone before applying application logic.



    Remember that time zones and offsets are not fixed and may change. For instance, historically US and UK used the same dates to 'spring forward' and 'fall back'.



    However, in 2007 the US changed the dates that the clocks get changed on. This now means that for 48 weeks of the year the difference between London time and New York time is 5 hours and for 4 weeks (3 in the spring, 1 in the autumn) it is 4 hours. Be aware of items like this in any calculations that involve multiple zones.



    Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, time zone offset and time zone name) you need to store for correct retrieval - see "Types of Time" in answer.



    Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.



    On servers, set hardware clocks and OS clocks to UTC rather than a local time zone.



    Regardless of the previous bullet point, server-side code, including web sites, should never expect the local time zone of the server to be anything in particular. see answer.



    Use NTP services on all servers.



    If using FAT32, remember that timestamps are stored in local time, not UTC.



    When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.



    Always query date-time values as lower-bound inclusive, upper-bound exclusive (>=, <).






    share|improve this answer























    • requires formatting...
      – Bhavin Ramani
      Jun 28 '16 at 5:11



















    0














    Laconic Answer (no need to change default timezone)



    $dateStr = '2008-09-11';
    $timezone = 'America/New_York';
    $time = strtotime(
    $dateStr,
    // convert timezone to offset seconds
    (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
    );


    Loquacious Answer



    Use strtotime's second option which changes the frame of reference of the function. By the way I prefer not to update the default time zone of the script:




    http://php.net/manual/en/function.strtotime.php



    int strtotime ( string $time [, int $now = time() ] )



    The function
    expects to be given a string containing an English date format and
    will try to parse that format into a Unix timestamp (the number of
    seconds since January 1 1970 00:00:00 UTC), relative to the timestamp
    given in now, or the current time if now is not supplied.




    And a Helper



    /**
    * Returns the timestamp of the provided time string using a specific timezone as the reference
    *
    * @param string $str
    * @param string $timezone
    * @return int number of the seconds
    */
    function strtotimetz($str, $timezone)
    {
    return strtotime(
    $str, strtotime(
    // convert timezone to offset seconds
    (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
    )
    );
    }

    var_export(
    date(
    'Y-m-d',
    strtotimetz('this monday', 'America/New_York')
    )
    );


    Maybe not the most performant approach, but works well when you know the default timezone and the offset. For example if the default timezone is UTC and the offset is -8 hours:



    var_dump(
    date(
    'Y-m-d',
    strtotime('this tuesday', strtotime(-8 . ' hours'))
    )
    );





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3569014%2fphp-strtotime-specify-timezone%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      69














      $date = new DateTime($dateStr, new DateTimeZone($timezone));


      $timestamp = $date->format('U');






      share|improve this answer



















      • 1




        Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
        – Andy Hin
        Aug 25 '10 at 18:26








      • 4




        @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
        – salathe
        Aug 25 '10 at 18:29










      • I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
        – Andy Hin
        Aug 25 '10 at 18:38








      • 7




        @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
        – Artefacto
        Aug 25 '10 at 18:51












      • @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
        – Andy Hin
        Aug 25 '10 at 19:05
















      69














      $date = new DateTime($dateStr, new DateTimeZone($timezone));


      $timestamp = $date->format('U');






      share|improve this answer



















      • 1




        Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
        – Andy Hin
        Aug 25 '10 at 18:26








      • 4




        @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
        – salathe
        Aug 25 '10 at 18:29










      • I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
        – Andy Hin
        Aug 25 '10 at 18:38








      • 7




        @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
        – Artefacto
        Aug 25 '10 at 18:51












      • @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
        – Andy Hin
        Aug 25 '10 at 19:05














      69












      69








      69






      $date = new DateTime($dateStr, new DateTimeZone($timezone));


      $timestamp = $date->format('U');






      share|improve this answer














      $date = new DateTime($dateStr, new DateTimeZone($timezone));


      $timestamp = $date->format('U');







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 19 '15 at 14:26









      Rupert

      1,056620




      1,056620










      answered Aug 25 '10 at 18:24









      salathe

      43.6k888119




      43.6k888119








      • 1




        Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
        – Andy Hin
        Aug 25 '10 at 18:26








      • 4




        @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
        – salathe
        Aug 25 '10 at 18:29










      • I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
        – Andy Hin
        Aug 25 '10 at 18:38








      • 7




        @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
        – Artefacto
        Aug 25 '10 at 18:51












      • @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
        – Andy Hin
        Aug 25 '10 at 19:05














      • 1




        Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
        – Andy Hin
        Aug 25 '10 at 18:26








      • 4




        @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
        – salathe
        Aug 25 '10 at 18:29










      • I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
        – Andy Hin
        Aug 25 '10 at 18:38








      • 7




        @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
        – Artefacto
        Aug 25 '10 at 18:51












      • @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
        – Andy Hin
        Aug 25 '10 at 19:05








      1




      1




      Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
      – Andy Hin
      Aug 25 '10 at 18:26






      Do I not need to specify what timezone I am converting from? Or does it just convert from PHP default?
      – Andy Hin
      Aug 25 '10 at 18:26






      4




      4




      @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
      – salathe
      Aug 25 '10 at 18:29




      @whydna you're not converting anything (unless I'm misreading). The code creates a representation of that date in the specified timezone.
      – salathe
      Aug 25 '10 at 18:29












      I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
      – Andy Hin
      Aug 25 '10 at 18:38






      I think you are converting. Because, it DOES matter what the original timezone for $dateStr is. So you need to somehow specify what timezone you are converting TO and what timezone you are converting FROM. But your example will work because the timezone it is converting FROM is the default php timezone.
      – Andy Hin
      Aug 25 '10 at 18:38






      7




      7




      @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
      – Artefacto
      Aug 25 '10 at 18:51






      @whydna No, you're not converting. You're interpreting the date string as if referred to the specified timezone. To convert you new $d = new DateTime($dateStr, $timezonefrom); $d->setTimezone($timezoneto);.
      – Artefacto
      Aug 25 '10 at 18:51














      @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
      – Andy Hin
      Aug 25 '10 at 19:05




      @Artefacto - if my $dateStr = '2008-09-11 00:00:00' does it not matter whether this original representation is in EST or PST? The timestamp would be different depending on which one it is, no? Maybe I am just confused :S
      – Andy Hin
      Aug 25 '10 at 19:05













      21














      The accepted answer is great if you're running PHP > 5.2 (I think that's the version they added the DateTime class). If you want to support an older version, you don't want to type as much, or if you just prefer the functional approach there is another way which also does not modify global settings:



      $dateStr = '2008-09-11 00:00:00';
      $timezone = 'America/New_York';
      $dtUtcDate = strtotime($dateStr. ' '. $timezone);





      share|improve this answer























      • I don't see how the accepted answer modifies global settings.
        – Sebastian Mach
        Aug 26 '15 at 11:54










      • It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
        – krowe
        Aug 26 '15 at 13:26


















      21














      The accepted answer is great if you're running PHP > 5.2 (I think that's the version they added the DateTime class). If you want to support an older version, you don't want to type as much, or if you just prefer the functional approach there is another way which also does not modify global settings:



      $dateStr = '2008-09-11 00:00:00';
      $timezone = 'America/New_York';
      $dtUtcDate = strtotime($dateStr. ' '. $timezone);





      share|improve this answer























      • I don't see how the accepted answer modifies global settings.
        – Sebastian Mach
        Aug 26 '15 at 11:54










      • It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
        – krowe
        Aug 26 '15 at 13:26
















      21












      21








      21






      The accepted answer is great if you're running PHP > 5.2 (I think that's the version they added the DateTime class). If you want to support an older version, you don't want to type as much, or if you just prefer the functional approach there is another way which also does not modify global settings:



      $dateStr = '2008-09-11 00:00:00';
      $timezone = 'America/New_York';
      $dtUtcDate = strtotime($dateStr. ' '. $timezone);





      share|improve this answer














      The accepted answer is great if you're running PHP > 5.2 (I think that's the version they added the DateTime class). If you want to support an older version, you don't want to type as much, or if you just prefer the functional approach there is another way which also does not modify global settings:



      $dateStr = '2008-09-11 00:00:00';
      $timezone = 'America/New_York';
      $dtUtcDate = strtotime($dateStr. ' '. $timezone);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 18 '15 at 22:09









      octern

      4,0901434




      4,0901434










      answered Jul 31 '13 at 13:31









      krowe

      1,6951115




      1,6951115












      • I don't see how the accepted answer modifies global settings.
        – Sebastian Mach
        Aug 26 '15 at 11:54










      • It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
        – krowe
        Aug 26 '15 at 13:26




















      • I don't see how the accepted answer modifies global settings.
        – Sebastian Mach
        Aug 26 '15 at 11:54










      • It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
        – krowe
        Aug 26 '15 at 13:26


















      I don't see how the accepted answer modifies global settings.
      – Sebastian Mach
      Aug 26 '15 at 11:54




      I don't see how the accepted answer modifies global settings.
      – Sebastian Mach
      Aug 26 '15 at 11:54












      It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
      – krowe
      Aug 26 '15 at 13:26






      It doesn't. It does use classes and requires PHP > 5.2. It was Jonahs' answer which modified global settings.
      – krowe
      Aug 26 '15 at 13:26













      16














      This will work if for some reason you're using <5.2 (Heaven forbid).



      $reset = date_default_timezone_get();
      date_default_timezone_set('America/New_York');
      $stamp = strtotime($dateStr);
      date_default_timezone_set($reset);


      But anything 5.2 and above, I'd strongly recommend you opt for @salathe's answer.






      share|improve this answer























      • works but dont forget to change it back! i think salathes solution is better
        – The Surrican
        Aug 25 '10 at 18:28










      • What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
        – Jonah
        Aug 25 '10 at 18:33










      • This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
        – Dan Heberden
        Aug 25 '10 at 19:26










      • Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
        – Fr0zenFyr
        Mar 15 '18 at 10:22
















      16














      This will work if for some reason you're using <5.2 (Heaven forbid).



      $reset = date_default_timezone_get();
      date_default_timezone_set('America/New_York');
      $stamp = strtotime($dateStr);
      date_default_timezone_set($reset);


      But anything 5.2 and above, I'd strongly recommend you opt for @salathe's answer.






      share|improve this answer























      • works but dont forget to change it back! i think salathes solution is better
        – The Surrican
        Aug 25 '10 at 18:28










      • What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
        – Jonah
        Aug 25 '10 at 18:33










      • This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
        – Dan Heberden
        Aug 25 '10 at 19:26










      • Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
        – Fr0zenFyr
        Mar 15 '18 at 10:22














      16












      16








      16






      This will work if for some reason you're using <5.2 (Heaven forbid).



      $reset = date_default_timezone_get();
      date_default_timezone_set('America/New_York');
      $stamp = strtotime($dateStr);
      date_default_timezone_set($reset);


      But anything 5.2 and above, I'd strongly recommend you opt for @salathe's answer.






      share|improve this answer














      This will work if for some reason you're using <5.2 (Heaven forbid).



      $reset = date_default_timezone_get();
      date_default_timezone_set('America/New_York');
      $stamp = strtotime($dateStr);
      date_default_timezone_set($reset);


      But anything 5.2 and above, I'd strongly recommend you opt for @salathe's answer.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 29 '15 at 23:00

























      answered Aug 25 '10 at 18:26









      Jonah

      7,56553570




      7,56553570












      • works but dont forget to change it back! i think salathes solution is better
        – The Surrican
        Aug 25 '10 at 18:28










      • What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
        – Jonah
        Aug 25 '10 at 18:33










      • This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
        – Dan Heberden
        Aug 25 '10 at 19:26










      • Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
        – Fr0zenFyr
        Mar 15 '18 at 10:22


















      • works but dont forget to change it back! i think salathes solution is better
        – The Surrican
        Aug 25 '10 at 18:28










      • What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
        – Jonah
        Aug 25 '10 at 18:33










      • This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
        – Dan Heberden
        Aug 25 '10 at 19:26










      • Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
        – Fr0zenFyr
        Mar 15 '18 at 10:22
















      works but dont forget to change it back! i think salathes solution is better
      – The Surrican
      Aug 25 '10 at 18:28




      works but dont forget to change it back! i think salathes solution is better
      – The Surrican
      Aug 25 '10 at 18:28












      What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
      – Jonah
      Aug 25 '10 at 18:33




      What do you mean "change it back"? Yeah, 1 line vs. 4 lines. Hmmm. :D
      – Jonah
      Aug 25 '10 at 18:33












      This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
      – Dan Heberden
      Aug 25 '10 at 19:26




      This solution (which isn't quite what the OP wants, which is why i made it a comment) is great if the entire application needs to run on a particular timezone for that process - just not near as elegant for a one time need.
      – Dan Heberden
      Aug 25 '10 at 19:26












      Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
      – Fr0zenFyr
      Mar 15 '18 at 10:22




      Sidenote: in version 5.5 and above, strtotime() will always give seconds since epoch for UTC. So, use DateTime class as already recommended in this answer
      – Fr0zenFyr
      Mar 15 '18 at 10:22











      3














      If you're going to use Timezones, I propose you use the DateTime class, and in this case the DateTime::createFromFormat() function which will allow you to do semothing like this:



      $start = "2015-01-14 11:59:43";
      $timezone = "America/Montreal";

      $tz = new DateTimeZone($timezone);
      $dt = DateTime::createFromFormat('Y-m-d H:i:s', $start, $tz);


      When you put $tz in the DateTime::createFromFormat function, you tell it what time zone the date you gave is in, so that when you need to convert it to another timezone, all you have to do is something like this:



      $start = $dt->setTimeZone(new DateTimeZone('UTC'));





      share|improve this answer


























        3














        If you're going to use Timezones, I propose you use the DateTime class, and in this case the DateTime::createFromFormat() function which will allow you to do semothing like this:



        $start = "2015-01-14 11:59:43";
        $timezone = "America/Montreal";

        $tz = new DateTimeZone($timezone);
        $dt = DateTime::createFromFormat('Y-m-d H:i:s', $start, $tz);


        When you put $tz in the DateTime::createFromFormat function, you tell it what time zone the date you gave is in, so that when you need to convert it to another timezone, all you have to do is something like this:



        $start = $dt->setTimeZone(new DateTimeZone('UTC'));





        share|improve this answer
























          3












          3








          3






          If you're going to use Timezones, I propose you use the DateTime class, and in this case the DateTime::createFromFormat() function which will allow you to do semothing like this:



          $start = "2015-01-14 11:59:43";
          $timezone = "America/Montreal";

          $tz = new DateTimeZone($timezone);
          $dt = DateTime::createFromFormat('Y-m-d H:i:s', $start, $tz);


          When you put $tz in the DateTime::createFromFormat function, you tell it what time zone the date you gave is in, so that when you need to convert it to another timezone, all you have to do is something like this:



          $start = $dt->setTimeZone(new DateTimeZone('UTC'));





          share|improve this answer












          If you're going to use Timezones, I propose you use the DateTime class, and in this case the DateTime::createFromFormat() function which will allow you to do semothing like this:



          $start = "2015-01-14 11:59:43";
          $timezone = "America/Montreal";

          $tz = new DateTimeZone($timezone);
          $dt = DateTime::createFromFormat('Y-m-d H:i:s', $start, $tz);


          When you put $tz in the DateTime::createFromFormat function, you tell it what time zone the date you gave is in, so that when you need to convert it to another timezone, all you have to do is something like this:



          $start = $dt->setTimeZone(new DateTimeZone('UTC'));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 14 '15 at 19:41









          Philippe Gilbert

          1509




          1509























              2














              Whenever you are referring to an exact moment in time, persist the time according to a unified standard that is not affected by daylight savings. (GMT and UTC are equivalent with this regard, but it is preferred to use the term UTC. Notice that UTC is also known as Zulu or Z time.)



              If instead you choose to persist a time using a local time value, include the local time offset from UTC, such that the timestamp can later be interpreted unambiguously.



              In some cases, you may need to store both the UTC time and the equivalent local time. Often this is done with two separate fields, but some platforms support a datetimeoffset type that can store both in a single field.



              When storing timestamps as a numeric value, use Unix time - which is the number of whole seconds since 1970-01-01T00:00:00Z (excluding leap seconds). If you require higher precision, use milliseconds instead. This value should always be based on UTC, without any time zone adjustment.



              If you might later need to modify the timestamp, include the original time zone ID so you can determine if the offset may have changed from the original value recorded.



              When scheduling future events, usually local time is preferred instead of UTC, as it is common for the offset to change. See answer, and blog post.



              Remember that time zone offsets are not always an integer number of hours (for example, Indian Standard Time is UTC+05:30, and Nepal uses UTC+05:45).



              If using Java, use java.time for Java 8, or use Joda Time for Java 7 or lower.
              If using .NET, consider using Noda Time.
              If using .NET without Noda Time, consider that DateTimeOffset is often a better choice than DateTime.
              If using Perl, use DateTime.
              If using Python, use pytz or dateutil.
              If using JavaScript, use moment.js with the moment-timezone extension.
              If using PHP > 5.2, use the native time zones conversions provided by DateTime, and DateTimeZone classes. Be careful when using.



              DateTimeZone::listAbbreviations() - see answer. To keep PHP with up to date Olson data, install periodically the timezonedb PECL package; see answer.



              If using C++, be sure to use a library that uses the properly implements the IANA timezone database. These include cctz, ICU, and Howard Hinnant's "tz" library.



              Do not use Boost for time zone conversions. While its API claims to support standard IANA (aka "zoneinfo") identifiers, it crudely maps them to fixed offsets without considering the rich history of changes each zone may have had.



              (Also, the file has fallen out of maintenance.)



              Most business rules use civil time, rather than UTC or GMT. Therefore, plan to convert UTC timestamps to a local time zone before applying application logic.



              Remember that time zones and offsets are not fixed and may change. For instance, historically US and UK used the same dates to 'spring forward' and 'fall back'.



              However, in 2007 the US changed the dates that the clocks get changed on. This now means that for 48 weeks of the year the difference between London time and New York time is 5 hours and for 4 weeks (3 in the spring, 1 in the autumn) it is 4 hours. Be aware of items like this in any calculations that involve multiple zones.



              Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, time zone offset and time zone name) you need to store for correct retrieval - see "Types of Time" in answer.



              Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.



              On servers, set hardware clocks and OS clocks to UTC rather than a local time zone.



              Regardless of the previous bullet point, server-side code, including web sites, should never expect the local time zone of the server to be anything in particular. see answer.



              Use NTP services on all servers.



              If using FAT32, remember that timestamps are stored in local time, not UTC.



              When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.



              Always query date-time values as lower-bound inclusive, upper-bound exclusive (>=, <).






              share|improve this answer























              • requires formatting...
                – Bhavin Ramani
                Jun 28 '16 at 5:11
















              2














              Whenever you are referring to an exact moment in time, persist the time according to a unified standard that is not affected by daylight savings. (GMT and UTC are equivalent with this regard, but it is preferred to use the term UTC. Notice that UTC is also known as Zulu or Z time.)



              If instead you choose to persist a time using a local time value, include the local time offset from UTC, such that the timestamp can later be interpreted unambiguously.



              In some cases, you may need to store both the UTC time and the equivalent local time. Often this is done with two separate fields, but some platforms support a datetimeoffset type that can store both in a single field.



              When storing timestamps as a numeric value, use Unix time - which is the number of whole seconds since 1970-01-01T00:00:00Z (excluding leap seconds). If you require higher precision, use milliseconds instead. This value should always be based on UTC, without any time zone adjustment.



              If you might later need to modify the timestamp, include the original time zone ID so you can determine if the offset may have changed from the original value recorded.



              When scheduling future events, usually local time is preferred instead of UTC, as it is common for the offset to change. See answer, and blog post.



              Remember that time zone offsets are not always an integer number of hours (for example, Indian Standard Time is UTC+05:30, and Nepal uses UTC+05:45).



              If using Java, use java.time for Java 8, or use Joda Time for Java 7 or lower.
              If using .NET, consider using Noda Time.
              If using .NET without Noda Time, consider that DateTimeOffset is often a better choice than DateTime.
              If using Perl, use DateTime.
              If using Python, use pytz or dateutil.
              If using JavaScript, use moment.js with the moment-timezone extension.
              If using PHP > 5.2, use the native time zones conversions provided by DateTime, and DateTimeZone classes. Be careful when using.



              DateTimeZone::listAbbreviations() - see answer. To keep PHP with up to date Olson data, install periodically the timezonedb PECL package; see answer.



              If using C++, be sure to use a library that uses the properly implements the IANA timezone database. These include cctz, ICU, and Howard Hinnant's "tz" library.



              Do not use Boost for time zone conversions. While its API claims to support standard IANA (aka "zoneinfo") identifiers, it crudely maps them to fixed offsets without considering the rich history of changes each zone may have had.



              (Also, the file has fallen out of maintenance.)



              Most business rules use civil time, rather than UTC or GMT. Therefore, plan to convert UTC timestamps to a local time zone before applying application logic.



              Remember that time zones and offsets are not fixed and may change. For instance, historically US and UK used the same dates to 'spring forward' and 'fall back'.



              However, in 2007 the US changed the dates that the clocks get changed on. This now means that for 48 weeks of the year the difference between London time and New York time is 5 hours and for 4 weeks (3 in the spring, 1 in the autumn) it is 4 hours. Be aware of items like this in any calculations that involve multiple zones.



              Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, time zone offset and time zone name) you need to store for correct retrieval - see "Types of Time" in answer.



              Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.



              On servers, set hardware clocks and OS clocks to UTC rather than a local time zone.



              Regardless of the previous bullet point, server-side code, including web sites, should never expect the local time zone of the server to be anything in particular. see answer.



              Use NTP services on all servers.



              If using FAT32, remember that timestamps are stored in local time, not UTC.



              When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.



              Always query date-time values as lower-bound inclusive, upper-bound exclusive (>=, <).






              share|improve this answer























              • requires formatting...
                – Bhavin Ramani
                Jun 28 '16 at 5:11














              2












              2








              2






              Whenever you are referring to an exact moment in time, persist the time according to a unified standard that is not affected by daylight savings. (GMT and UTC are equivalent with this regard, but it is preferred to use the term UTC. Notice that UTC is also known as Zulu or Z time.)



              If instead you choose to persist a time using a local time value, include the local time offset from UTC, such that the timestamp can later be interpreted unambiguously.



              In some cases, you may need to store both the UTC time and the equivalent local time. Often this is done with two separate fields, but some platforms support a datetimeoffset type that can store both in a single field.



              When storing timestamps as a numeric value, use Unix time - which is the number of whole seconds since 1970-01-01T00:00:00Z (excluding leap seconds). If you require higher precision, use milliseconds instead. This value should always be based on UTC, without any time zone adjustment.



              If you might later need to modify the timestamp, include the original time zone ID so you can determine if the offset may have changed from the original value recorded.



              When scheduling future events, usually local time is preferred instead of UTC, as it is common for the offset to change. See answer, and blog post.



              Remember that time zone offsets are not always an integer number of hours (for example, Indian Standard Time is UTC+05:30, and Nepal uses UTC+05:45).



              If using Java, use java.time for Java 8, or use Joda Time for Java 7 or lower.
              If using .NET, consider using Noda Time.
              If using .NET without Noda Time, consider that DateTimeOffset is often a better choice than DateTime.
              If using Perl, use DateTime.
              If using Python, use pytz or dateutil.
              If using JavaScript, use moment.js with the moment-timezone extension.
              If using PHP > 5.2, use the native time zones conversions provided by DateTime, and DateTimeZone classes. Be careful when using.



              DateTimeZone::listAbbreviations() - see answer. To keep PHP with up to date Olson data, install periodically the timezonedb PECL package; see answer.



              If using C++, be sure to use a library that uses the properly implements the IANA timezone database. These include cctz, ICU, and Howard Hinnant's "tz" library.



              Do not use Boost for time zone conversions. While its API claims to support standard IANA (aka "zoneinfo") identifiers, it crudely maps them to fixed offsets without considering the rich history of changes each zone may have had.



              (Also, the file has fallen out of maintenance.)



              Most business rules use civil time, rather than UTC or GMT. Therefore, plan to convert UTC timestamps to a local time zone before applying application logic.



              Remember that time zones and offsets are not fixed and may change. For instance, historically US and UK used the same dates to 'spring forward' and 'fall back'.



              However, in 2007 the US changed the dates that the clocks get changed on. This now means that for 48 weeks of the year the difference between London time and New York time is 5 hours and for 4 weeks (3 in the spring, 1 in the autumn) it is 4 hours. Be aware of items like this in any calculations that involve multiple zones.



              Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, time zone offset and time zone name) you need to store for correct retrieval - see "Types of Time" in answer.



              Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.



              On servers, set hardware clocks and OS clocks to UTC rather than a local time zone.



              Regardless of the previous bullet point, server-side code, including web sites, should never expect the local time zone of the server to be anything in particular. see answer.



              Use NTP services on all servers.



              If using FAT32, remember that timestamps are stored in local time, not UTC.



              When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.



              Always query date-time values as lower-bound inclusive, upper-bound exclusive (>=, <).






              share|improve this answer














              Whenever you are referring to an exact moment in time, persist the time according to a unified standard that is not affected by daylight savings. (GMT and UTC are equivalent with this regard, but it is preferred to use the term UTC. Notice that UTC is also known as Zulu or Z time.)



              If instead you choose to persist a time using a local time value, include the local time offset from UTC, such that the timestamp can later be interpreted unambiguously.



              In some cases, you may need to store both the UTC time and the equivalent local time. Often this is done with two separate fields, but some platforms support a datetimeoffset type that can store both in a single field.



              When storing timestamps as a numeric value, use Unix time - which is the number of whole seconds since 1970-01-01T00:00:00Z (excluding leap seconds). If you require higher precision, use milliseconds instead. This value should always be based on UTC, without any time zone adjustment.



              If you might later need to modify the timestamp, include the original time zone ID so you can determine if the offset may have changed from the original value recorded.



              When scheduling future events, usually local time is preferred instead of UTC, as it is common for the offset to change. See answer, and blog post.



              Remember that time zone offsets are not always an integer number of hours (for example, Indian Standard Time is UTC+05:30, and Nepal uses UTC+05:45).



              If using Java, use java.time for Java 8, or use Joda Time for Java 7 or lower.
              If using .NET, consider using Noda Time.
              If using .NET without Noda Time, consider that DateTimeOffset is often a better choice than DateTime.
              If using Perl, use DateTime.
              If using Python, use pytz or dateutil.
              If using JavaScript, use moment.js with the moment-timezone extension.
              If using PHP > 5.2, use the native time zones conversions provided by DateTime, and DateTimeZone classes. Be careful when using.



              DateTimeZone::listAbbreviations() - see answer. To keep PHP with up to date Olson data, install periodically the timezonedb PECL package; see answer.



              If using C++, be sure to use a library that uses the properly implements the IANA timezone database. These include cctz, ICU, and Howard Hinnant's "tz" library.



              Do not use Boost for time zone conversions. While its API claims to support standard IANA (aka "zoneinfo") identifiers, it crudely maps them to fixed offsets without considering the rich history of changes each zone may have had.



              (Also, the file has fallen out of maintenance.)



              Most business rules use civil time, rather than UTC or GMT. Therefore, plan to convert UTC timestamps to a local time zone before applying application logic.



              Remember that time zones and offsets are not fixed and may change. For instance, historically US and UK used the same dates to 'spring forward' and 'fall back'.



              However, in 2007 the US changed the dates that the clocks get changed on. This now means that for 48 weeks of the year the difference between London time and New York time is 5 hours and for 4 weeks (3 in the spring, 1 in the autumn) it is 4 hours. Be aware of items like this in any calculations that involve multiple zones.



              Consider the type of time (actual event time, broadcast time, relative time, historical time, recurring time) what elements (timestamp, time zone offset and time zone name) you need to store for correct retrieval - see "Types of Time" in answer.



              Keep your OS, database and application tzdata files in sync, between themselves and the rest of the world.



              On servers, set hardware clocks and OS clocks to UTC rather than a local time zone.



              Regardless of the previous bullet point, server-side code, including web sites, should never expect the local time zone of the server to be anything in particular. see answer.



              Use NTP services on all servers.



              If using FAT32, remember that timestamps are stored in local time, not UTC.



              When dealing with recurring events (weekly TV show, for example), remember that the time changes with DST and will be different across time zones.



              Always query date-time values as lower-bound inclusive, upper-bound exclusive (>=, <).







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 28 '16 at 5:54









              M.R.Safari

              1,1321933




              1,1321933










              answered Jun 28 '16 at 5:06









              Charu

              211




              211












              • requires formatting...
                – Bhavin Ramani
                Jun 28 '16 at 5:11


















              • requires formatting...
                – Bhavin Ramani
                Jun 28 '16 at 5:11
















              requires formatting...
              – Bhavin Ramani
              Jun 28 '16 at 5:11




              requires formatting...
              – Bhavin Ramani
              Jun 28 '16 at 5:11











              0














              Laconic Answer (no need to change default timezone)



              $dateStr = '2008-09-11';
              $timezone = 'America/New_York';
              $time = strtotime(
              $dateStr,
              // convert timezone to offset seconds
              (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
              );


              Loquacious Answer



              Use strtotime's second option which changes the frame of reference of the function. By the way I prefer not to update the default time zone of the script:




              http://php.net/manual/en/function.strtotime.php



              int strtotime ( string $time [, int $now = time() ] )



              The function
              expects to be given a string containing an English date format and
              will try to parse that format into a Unix timestamp (the number of
              seconds since January 1 1970 00:00:00 UTC), relative to the timestamp
              given in now, or the current time if now is not supplied.




              And a Helper



              /**
              * Returns the timestamp of the provided time string using a specific timezone as the reference
              *
              * @param string $str
              * @param string $timezone
              * @return int number of the seconds
              */
              function strtotimetz($str, $timezone)
              {
              return strtotime(
              $str, strtotime(
              // convert timezone to offset seconds
              (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
              )
              );
              }

              var_export(
              date(
              'Y-m-d',
              strtotimetz('this monday', 'America/New_York')
              )
              );


              Maybe not the most performant approach, but works well when you know the default timezone and the offset. For example if the default timezone is UTC and the offset is -8 hours:



              var_dump(
              date(
              'Y-m-d',
              strtotime('this tuesday', strtotime(-8 . ' hours'))
              )
              );





              share|improve this answer




























                0














                Laconic Answer (no need to change default timezone)



                $dateStr = '2008-09-11';
                $timezone = 'America/New_York';
                $time = strtotime(
                $dateStr,
                // convert timezone to offset seconds
                (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                );


                Loquacious Answer



                Use strtotime's second option which changes the frame of reference of the function. By the way I prefer not to update the default time zone of the script:




                http://php.net/manual/en/function.strtotime.php



                int strtotime ( string $time [, int $now = time() ] )



                The function
                expects to be given a string containing an English date format and
                will try to parse that format into a Unix timestamp (the number of
                seconds since January 1 1970 00:00:00 UTC), relative to the timestamp
                given in now, or the current time if now is not supplied.




                And a Helper



                /**
                * Returns the timestamp of the provided time string using a specific timezone as the reference
                *
                * @param string $str
                * @param string $timezone
                * @return int number of the seconds
                */
                function strtotimetz($str, $timezone)
                {
                return strtotime(
                $str, strtotime(
                // convert timezone to offset seconds
                (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                )
                );
                }

                var_export(
                date(
                'Y-m-d',
                strtotimetz('this monday', 'America/New_York')
                )
                );


                Maybe not the most performant approach, but works well when you know the default timezone and the offset. For example if the default timezone is UTC and the offset is -8 hours:



                var_dump(
                date(
                'Y-m-d',
                strtotime('this tuesday', strtotime(-8 . ' hours'))
                )
                );





                share|improve this answer


























                  0












                  0








                  0






                  Laconic Answer (no need to change default timezone)



                  $dateStr = '2008-09-11';
                  $timezone = 'America/New_York';
                  $time = strtotime(
                  $dateStr,
                  // convert timezone to offset seconds
                  (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                  );


                  Loquacious Answer



                  Use strtotime's second option which changes the frame of reference of the function. By the way I prefer not to update the default time zone of the script:




                  http://php.net/manual/en/function.strtotime.php



                  int strtotime ( string $time [, int $now = time() ] )



                  The function
                  expects to be given a string containing an English date format and
                  will try to parse that format into a Unix timestamp (the number of
                  seconds since January 1 1970 00:00:00 UTC), relative to the timestamp
                  given in now, or the current time if now is not supplied.




                  And a Helper



                  /**
                  * Returns the timestamp of the provided time string using a specific timezone as the reference
                  *
                  * @param string $str
                  * @param string $timezone
                  * @return int number of the seconds
                  */
                  function strtotimetz($str, $timezone)
                  {
                  return strtotime(
                  $str, strtotime(
                  // convert timezone to offset seconds
                  (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                  )
                  );
                  }

                  var_export(
                  date(
                  'Y-m-d',
                  strtotimetz('this monday', 'America/New_York')
                  )
                  );


                  Maybe not the most performant approach, but works well when you know the default timezone and the offset. For example if the default timezone is UTC and the offset is -8 hours:



                  var_dump(
                  date(
                  'Y-m-d',
                  strtotime('this tuesday', strtotime(-8 . ' hours'))
                  )
                  );





                  share|improve this answer














                  Laconic Answer (no need to change default timezone)



                  $dateStr = '2008-09-11';
                  $timezone = 'America/New_York';
                  $time = strtotime(
                  $dateStr,
                  // convert timezone to offset seconds
                  (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                  );


                  Loquacious Answer



                  Use strtotime's second option which changes the frame of reference of the function. By the way I prefer not to update the default time zone of the script:




                  http://php.net/manual/en/function.strtotime.php



                  int strtotime ( string $time [, int $now = time() ] )



                  The function
                  expects to be given a string containing an English date format and
                  will try to parse that format into a Unix timestamp (the number of
                  seconds since January 1 1970 00:00:00 UTC), relative to the timestamp
                  given in now, or the current time if now is not supplied.




                  And a Helper



                  /**
                  * Returns the timestamp of the provided time string using a specific timezone as the reference
                  *
                  * @param string $str
                  * @param string $timezone
                  * @return int number of the seconds
                  */
                  function strtotimetz($str, $timezone)
                  {
                  return strtotime(
                  $str, strtotime(
                  // convert timezone to offset seconds
                  (new DateTimeZone($timezone))->getOffset(new DateTime) - (new DateTimeZone(date_default_timezone_get()))->getOffset(new DateTime) . ' seconds'
                  )
                  );
                  }

                  var_export(
                  date(
                  'Y-m-d',
                  strtotimetz('this monday', 'America/New_York')
                  )
                  );


                  Maybe not the most performant approach, but works well when you know the default timezone and the offset. For example if the default timezone is UTC and the offset is -8 hours:



                  var_dump(
                  date(
                  'Y-m-d',
                  strtotime('this tuesday', strtotime(-8 . ' hours'))
                  )
                  );






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 8:54

























                  answered Nov 21 '18 at 7:12









                  HPM

                  1,06911840




                  1,06911840






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3569014%2fphp-strtotime-specify-timezone%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Marschland