peterkahl/world-date

Replacement for the PHP `date()` function. Returns properly formatted date (time) string including correct timezone abbreviation, full timezone name.

v2.0.1 2019-04-04 06:19 UTC

README

Downloads GitHub If this project has business value for you then don't hesitate to support me with a small donation.

Replacement for the PHP date() function. World timezones, abbreviations, full names of zones.

Returns properly formatted date (time) string including correct timezone abbreviation (e.g. '2019-03-02 23:31:50 EST -0500'). Non-standard format character Q returns the full timezone name (e.g. 'Gulf Standard Time').

One of the reasons which inspired creation of this library is that PHP uses Linux's tzdata which lacks most timezone abbreviations.

image

The Problem 💩

PHP date('T') will not always return timezone abbreviation.

$DateObj = new DateTime('now');

$DateObj->setTimeZone(new DateTimeZone('Europe/London');
echo $DateObj->format('T'); # Prints 'GMT' as it should, but ...

$DateObj->setTimeZone(new DateTimeZone('Asia/Dubai');
echo $DateObj->format('T'); # Prints '+0400' although you would expect 'GST'!

# This is because PHP uses Linux's tzdata which lacks most timezone abbreviations.

The Solution 👏🏻🥳🍾🎉

use peterkahl\WorldDate\WorldDate;

$wDate = new WorldDate;



# Get formatted time string for GMT zone (current time, i.e. current epoch):
echo $wDate->GetFormattedTimeString(); # 2019-03-03 08:06:18 GMT +0000


# Current time in Moscow:
echo $wDate->GetFormattedTimeString('Europe/Moscow'); # 2019-03-03 11:06:18 MSK +0300


# Current time spelled out:
echo $wDate->GetFormattedTimeString('Europe/Moscow', 'now', 'H:i Q'); # 11:06 Moscow Time
echo $wDate->GetFormattedTimeString('Europe/Paris', 'now', 'H:i Q');  # 09:06 Central European Time



# Get formatted time string for given zone (arbitrary epoch):
echo $wDate->GetFormattedTimeString('Asia/Tokyo', 1553817600);        # 2019-03-29 09:00:00 JST +0900

echo $wDate->GetFormattedTimeString('Asia/Novosibirsk', 1553817600);  # 2019-03-29 07:00:00 NOVT +0700

echo $wDate->GetFormattedTimeString('Europe/London', 1553817600);     # 2019-03-29 00:00:00 GMT +0000

echo $wDate->GetFormattedTimeString('America/Santiago', 1553817600);  # 2019-03-28 21:00:00 CLST -0300

# Define your own date format:
echo $wDate->GetFormattedTimeString('America/Santiago', 1553817600, 'H:i:s T'); # 21:00:00 CLST



# The non-standard format character 'Q' will produce full name of timezone:
echo $wDate->GetFormattedTimeString('Asia/Novosibirsk', 1553817600, 'F j, Y, g:i a (Q)');
# March 29, 2019, 7:00 am (Novosibirsk Standard Time)


echo $wDate->GetFormattedTimeString('Europe/London', 1553817600, 'j F Y, g:i a Q');
# 29 March 2019, 0:00 am Greenwich Mean Time



# Get formatted time string for given zone (current time, i.e. current epoch):
echo $wDate->GetFormattedTimeString('Asia/Hong_Kong');      # 2019-03-03 12:40:24 HKT +0800

echo $wDate->GetFormattedTimeString('Asia/Tehran');         # 2019-03-03 08:10:24 IRST +0330

echo $wDate->GetFormattedTimeString('Europe/Rome');         # 2019-03-03 05:40:24 CET +0100

echo $wDate->GetFormattedTimeString('America/New_York');    # 2019-03-02 23:40:24 EST -0500

echo $wDate->GetFormattedTimeString('America/Los_Angeles'); # 2019-03-02 20:40:24 PST -0800

# Define your own date format:
echo $wDate->GetFormattedTimeString('America/Los_Angeles', 'now', 'H:i:s T (Q)');
# 20:40:24 PST (Pacific Standard Time)



# Get abbreviation from zone db name:
echo $wDate->GetAbbr('Asia/Dubai');             # GST
# ...or for specific epoch
echo $wDate->GetAbbr('Asia/Dubai', 1553817600); # GST
# ...or the same using:
echo $wDate->GetFormattedTimeString('Asia/Dubai', 'now', 'T'); # GST


# Get full name from zone db name:
echo $wDate->GetName('Asia/Dubai');             # Gulf Standard Time
# ...or the same using:
echo $wDate->GetFormattedTimeString('Asia/Dubai', 1553817600, 'Q'); # Gulf Standard Time
# ...or the same using:
echo $wDate->GetFormattedTimeString('Asia/Dubai', 'now', 'Q');      # Gulf Standard Time