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

- 4,228
- 28
- 39

- 13,389
- 27
- 80
- 110
-
You should definitely accept [DesignerGuy's answer](https://stackoverflow.com/a/24394179/3931192) or change the title of your question to something like "set the default timezone"... – Axel Apr 24 '19 at 09:51
-
1@Axel Done, I'm not a PHP dev anymore, but I hopes it helps. – einstein Apr 25 '19 at 10:08
-
Well Done! It was the right decision IMHO... – Axel Apr 25 '19 at 10:46
5 Answers
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.
Here's the short version:
<?php
$date = new DateTime("now", new DateTimeZone('America/New_York') );
echo $date->format('Y-m-d H:i:s');
Works in PHP >= 5.2.0
List of supported timezones: 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 "\n<br />\n";
// 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');
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.
I'm sure there are a number of other libraries available, but these are a few I'm familiar with.
Bonus Lesson: Immutable Date Objects
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') . "\n<br>";
echo "---\n<br>";
$oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days'));
$twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));
echo $today->format('Y-m-d') . "\n<br>";
echo $oneWeekFromToday->format('Y-m-d') . "\n<br>";
echo $twoWeeksFromToday->format('Y-m-d') . "\n<br>";
echo "\n<br>";
The output:
2017-02-11
---
2017-03-04
2017-03-04
2017-03-04
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.
This is where DateTimeImmutable
comes in.
$today = new DateTimeImmutable();
echo $today->format('Y-m-d') . "\n<br>";
echo "---\n<br>";
$oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days'));
$twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));
echo $today->format('Y-m-d') . "\n<br>";
echo $oneWeekFromToday->format('Y-m-d') . "\n<br>";
echo $twoWeeksFromToday->format('Y-m-d') . "\n<br>";
The output:
2017-02-11
---
2017-02-11
2017-02-18
2017-02-25
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.

- 7,655
- 6
- 33
- 53
-
Appreciate the answer @DesignerGuy, but here's a *proper* list of supported timezones: http://php.net/manual/en/timezones.php – Chris Harrison Jun 24 '15 at 08:16
-
This was helpful. But I'm unable to get the short version working. It works when the datetime string is "now", but not when the string is a specific datetime. The verbose version works fine as written. MAMP Pro, PHP 7.4.12. – lflier Mar 28 '22 at 14:07
-
Good answer. I'd just like to add that DateTime (i.e. the non-immutable version) should not be discounted. It's a more object oriented approach. You can achieve the same result as your example with DateTime objects by using the clone keyword and modifying the cloned copy. – Mark Jun 14 '22 at 02:10
Set the default time zone first and get the date then, the date will be in the time zone you specify :
<?php
date_default_timezone_set('America/New_York');
$date= date('m-d-Y') ;
?>
http://php.net/manual/en/function.date-default-timezone-set.php

- 2,179
- 1
- 21
- 25
-
12Note that using `date_default_timezone_set()` sets it for all usage of `date()` after that point. See my answer for an example of how to manage multiple timezones. – Andy Fleming Jul 07 '14 at 05:45
-
9Currently bad practice if you just want to get 1 date in that timezone, see DesignerGuy's answer below for a more fleshed out solution. – Ward D.S. Nov 27 '15 at 12:12
-
-
So this is a great solution for countries with winter and summer time (and if you want only one time zone in your project), right? :D – Numb3rs May 04 '22 at 09:37
I have created some simple function you can use to convert time to any timezone :
function convertTimeToLocal($datetime,$timezone='Europe/Dublin') {
$given = new DateTime($datetime, new DateTimeZone("UTC"));
$given->setTimezone(new DateTimeZone($timezone));
$output = $given->format("Y-m-d"); //can change as per your requirement
return $output;
}

- 567
- 8
- 19
If you have access to PHP 5.3, the intl extension is very nice for doing things like this.
Here's an example from the manual:
$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
$fmt->format(0); //0 for current time/date
In your case, you can do:
$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/New_York');
$fmt->format($datetime); //where $datetime may be a DateTime object, an integer representing a Unix timestamp value (seconds since epoch, UTC) or an array in the format output by localtime().
As you can set a Timezone such as America/New_York
, this is much better than using a GMT or UTC offset, as this takes into account the day light savings periods as well.
Finaly, as the intl extension uses ICU data, which contains a lot of very useful features when it comes to creating your own date/time formats.

- 32,163
- 26
- 99
- 170
<?php
date_default_timezone_set('GMT-5');//Set New York timezone
$today = date("F j, Y")
?>

- 3,004
- 15
- 52
- 79