<i id='TRoIW'><tr id='TRoIW'><dt id='TRoIW'><q id='TRoIW'><span id='TRoIW'><b id='TRoIW'><form id='TRoIW'><ins id='TRoIW'></ins><ul id='TRoIW'></ul><sub id='TRoIW'></sub></form><legend id='TRoIW'></legend><bdo id='TRoIW'><pre id='TRoIW'><center id='TRoIW'></center></pre></bdo></b><th id='TRoIW'></th></span></q></dt></tr></i><div id='TRoIW'><tfoot id='TRoIW'></tfoot><dl id='TRoIW'><fieldset id='TRoIW'></fieldset></dl></div>

      <legend id='TRoIW'><style id='TRoIW'><dir id='TRoIW'><q id='TRoIW'></q></dir></style></legend>

      • <bdo id='TRoIW'></bdo><ul id='TRoIW'></ul>

      <small id='TRoIW'></small><noframes id='TRoIW'>

    1. <tfoot id='TRoIW'></tfoot>
      1. 获取当前日期,给定 PHP 中的时区?

        时间:2024-08-15

                <tbody id='KiOzP'></tbody>
              <legend id='KiOzP'><style id='KiOzP'><dir id='KiOzP'><q id='KiOzP'></q></dir></style></legend><tfoot id='KiOzP'></tfoot>
            • <i id='KiOzP'><tr id='KiOzP'><dt id='KiOzP'><q id='KiOzP'><span id='KiOzP'><b id='KiOzP'><form id='KiOzP'><ins id='KiOzP'></ins><ul id='KiOzP'></ul><sub id='KiOzP'></sub></form><legend id='KiOzP'></legend><bdo id='KiOzP'><pre id='KiOzP'><center id='KiOzP'></center></pre></bdo></b><th id='KiOzP'></th></span></q></dt></tr></i><div id='KiOzP'><tfoot id='KiOzP'></tfoot><dl id='KiOzP'><fieldset id='KiOzP'></fieldset></dl></div>
              • <small id='KiOzP'></small><noframes id='KiOzP'>

                  <bdo id='KiOzP'></bdo><ul id='KiOzP'></ul>
                • 本文介绍了获取当前日期,给定 PHP 中的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 PHP 中以 Paul Eggert 格式(America/New_York)的时区获取今天的日期?

                  I want to get todays date given a time zone in Paul Eggert format(America/New_York) in PHP?

                  推荐答案

                  其他答案设置系统中所有日期的时区.如果您想为用户支持多个时区,这并不总是有效.

                  The other answers set the timezone for all dates in your system. This doesn't always work well if you want to support multiple timezones for your users.

                  这是简短的版本:

                  <?php
                  $date = new DateTime("now", new DateTimeZone('America/New_York') );
                  echo $date->format('Y-m-d H:i:s');
                  

                  适用于 PHP >= 5.2.0

                  Works in PHP >= 5.2.0

                  支持的时区列表:php.net/manual/en/timezones.php

                  这是一个有现有时间并通过用户设置设置时区的版本

                  Here's a version with an existing time and setting timezone by a user setting

                  <?php
                  
                  $usersTimezone = 'America/New_York';
                  $date = new DateTime( 'Thu, 31 Mar 2011 02:05:59 GMT', new DateTimeZone($usersTimezone) );
                  echo $date->format('Y-m-d H:i:s');
                  

                  <小时>

                  这里有一个更详细的版本,可以更清楚地展示这个过程


                  Here is a more verbose version to show the process a little more clearly

                  <?php
                  
                  // Date for a specific date/time:
                  $date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
                  
                  // Output date (as-is)
                  echo $date->format('l, F j Y g:i:s A');     
                  
                  // Output line break (for testing)
                  echo "
                  <br />
                  ";
                  
                  // Example user timezone (to show it can be used dynamically)
                  $usersTimezone = 'America/New_York';
                  
                  // Convert timezone
                  $tz = new DateTimeZone($usersTimezone);
                  $date->setTimeZone($tz);
                  
                  // Output date after 
                  echo $date->format('l, F j Y g:i:s A');
                  

                  <小时>

                  • Carbon — 一个非常流行的日期库.
                  • Chronos — 专注于不变性的 Carbon 替代品.请参阅下文,了解为什么这很重要.
                  • jenssegers/date — Carbon 的扩展,增加了多语言支持.

                  • Libraries

                    • Carbon — A very popular date library.
                    • Chronos — A drop-in replacement for Carbon focused on immutability. See below on why that's important.
                    • jenssegers/date — An extension of Carbon that adds multi-language support.
                    • 我确信还有许多其他可用的库,但这些是我熟悉的一些.

                      当你在这里的时候,让我为你省去一些未来的头痛.假设您要计算从今天开始的 1 周和从今天开始的 2 周.你可能会写一些类似的代码:

                      While you're here, let me save you some future headache. Let's say you want to calculate 1 week from today and 2 weeks from today. You might write some code like:

                      <?php
                      
                      // Create a datetime (now, in this case 2017-Feb-11)
                      $today = new DateTime();
                      
                      echo $today->format('Y-m-d') . "
                      <br>";
                      echo "---
                      <br>";
                      
                      $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days'));
                      $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));
                      
                      echo $today->format('Y-m-d') . "
                      <br>";
                      echo $oneWeekFromToday->format('Y-m-d') . "
                      <br>";
                      echo $twoWeeksFromToday->format('Y-m-d') . "
                      <br>";
                      echo "
                      <br>";
                      

                      输出:

                      2017-02-11 
                      --- 
                      2017-03-04 
                      2017-03-04 
                      2017-03-04
                      

                      嗯...这不是我们想要的.在 PHP 中修改传统的 DateTime 对象不仅会返回更新的日期,还会修改原始对象.

                      Hmmmm... That's not quite what we wanted. Modifying a traditional DateTime object in PHP not only returns the updated date but modifies the original object as well.

                      这就是 DateTimeImmutable 的用武之地.

                      This is where DateTimeImmutable comes in.

                      $today = new DateTimeImmutable();
                      
                      echo $today->format('Y-m-d') . "
                      <br>";
                      echo "---
                      <br>";
                      
                      $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days'));
                      $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));
                      
                      echo $today->format('Y-m-d') . "
                      <br>";
                      echo $oneWeekFromToday->format('Y-m-d') . "
                      <br>";
                      echo $twoWeeksFromToday->format('Y-m-d') . "
                      <br>";
                      

                      输出:

                      2017-02-11 
                      --- 
                      2017-02-11 
                      2017-02-18 
                      2017-02-25 
                      

                      在第二个示例中,我们得到了预期的日期.通过使用 DateTimeImmutable 而不是 DateTime,我们可以防止意外的状态突变并防止潜在的错误.

                      In this second example, we get the dates we expected back. By using DateTimeImmutable instead of DateTime, we prevent accidental state mutations and prevent potential bugs.

                      这篇关于获取当前日期,给定 PHP 中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP 使用的美国时区列表? 下一篇:PHP - strtotime,指定时区

                  相关文章

                  <i id='K4M9y'><tr id='K4M9y'><dt id='K4M9y'><q id='K4M9y'><span id='K4M9y'><b id='K4M9y'><form id='K4M9y'><ins id='K4M9y'></ins><ul id='K4M9y'></ul><sub id='K4M9y'></sub></form><legend id='K4M9y'></legend><bdo id='K4M9y'><pre id='K4M9y'><center id='K4M9y'></center></pre></bdo></b><th id='K4M9y'></th></span></q></dt></tr></i><div id='K4M9y'><tfoot id='K4M9y'></tfoot><dl id='K4M9y'><fieldset id='K4M9y'></fieldset></dl></div>
                  • <bdo id='K4M9y'></bdo><ul id='K4M9y'></ul>

                      <small id='K4M9y'></small><noframes id='K4M9y'>

                    1. <legend id='K4M9y'><style id='K4M9y'><dir id='K4M9y'><q id='K4M9y'></q></dir></style></legend>

                      <tfoot id='K4M9y'></tfoot>