获取当前日期,给定 PHP 中的时区?

2022-01-16 00:00:00 date timezone 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.

相关文章