2

I have the following PHP date function and it returns "UTC" as the timezone. I need it to return the time zone for America/New York either EST or EDT as appropriate.

date( 'D, d M Y H:i:s T' );

It returns: Thu, 08 Oct 2015 16:48:00 UTC

I need it to return Thu, 08 Oct 2015 16:48:00 EDT

Amended.

Here is my complete PHP code line:

date_default_timezone_set( 'America/New_York' );
echo '<meta http-equiv="last-modified" content="' . date( 'D, d M Y H:i:s T', strtotime( get_the_date() .' ' . get_the_time() ) ) . '" />' . "\n";

It is part of a WordPress plugin whose purpose is to insert a meta tag in the head container to reflect the date and time the WP Post was last updated.

S.I.
  • 3,250
  • 12
  • 48
  • 77
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • Set the timezone before calling date() – GordonM Nov 08 '16 at 19:04
  • [set the timezone](http://php.net/manual/en/function.date-default-timezone-set.php) then call date – Jay Blanchard Nov 08 '16 at 19:05
  • 1
    `$estTime = (new DateTime('America/New_York'))->format('h:i:s:u');` : http://stackoverflow.com/a/22393061/1158599 – S.I. Nov 08 '16 at 19:05
  • http://stackoverflow.com/questions/20288789/php-date-with-timezone – Jeremy Harris Nov 08 '16 at 19:06
  • But isn't that sort of "hardcoding the Time Zone @S.I. ? How do I handle dates & times in the EDT zone vs. dates & times in the EST zone? – H. Ferrence Nov 08 '16 at 19:17
  • I guess it is sort of.. but you should not have problems later at some point to define new datetime.. – S.I. Nov 08 '16 at 19:19
  • Not sure I understand your last comment @S.I. My PHP function returns the date and time stamp of last file save in the file system. So I need the function to be smart enough to know whether the date & time stamp being returned is EDT or EST. Hope that makes better sense... – H. Ferrence Nov 08 '16 at 19:22
  • I'm sorry but if is timestamp I don't understand how you know who and from where is saved this file. timestamp is timestamp.. – S.I. Nov 08 '16 at 19:25
  • I'll update my OQ to show the whole function. I elected to just show the portion of the PHP code line that pertained to Time Zone. One moment... – H. Ferrence Nov 08 '16 at 19:27
  • Have a look at my amended OQ @S.I. See if that clarifies. – H. Ferrence Nov 08 '16 at 19:29
  • 1
    @H.Ferrence [check this](http://sandbox.onlinephpfunctions.com/code/b6ae303227730c79fa3372fa68505b44023e1862). Maybe will help.. – S.I. Nov 08 '16 at 19:48
  • Awesome and perfect @S.I. That does exactly what I needed it too. Move it to an answer and I'll accept it! – H. Ferrence Nov 08 '16 at 20:28
  • @H.Ferrence thank's for the feedback. Answer added. – S.I. Nov 09 '16 at 04:44

1 Answers1

1

First defining of default time zone to America/New_York then you can use RFC850 to show the date in the desired format

DATE_RFC850

This is the format for RFC850 which defines the standards for USENET messages. The PHP format is "l, d-M-y H:i:s T" and example output from date(DATE_RFC850) is "Sunday, 14-Aug-05 16:13:03 UTC".

Example of usage

date_default_timezone_set('America/New_York');
$a = strtotime('2015-08-11 16:48:00');
print date(DATE_RFC850, $a);

// output
Tuesday, 11-Aug-15 16:48:00 EDT

Working demo

It is possible also to add default time zone in your php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
; date.timezone =
Community
  • 1
  • 1
S.I.
  • 3,250
  • 12
  • 48
  • 77