PHP Classes
elePHPant
Icontem

PHP World Date: Prints formatted date string

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-04-03 (21 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 117 This week: 1All time: 9,008 This week: 345Up
Version License PHP version Categories
world-date 1.0Custom (specified...5PHP 5, Time and Date, Geography
Description Author

This class is an enhanced replacement for the PHP date() function.

It returns time for any world timezone, including zone abbreviations and fully spelled out names of zones.

It takes as a parameter string with the name of a timezone (and optionally other parameters).

The class can also retrieve a list of known timezone names from a JSON configuration file and returns the given timezone full name, the abbreviated name and offset from UTC.

Innovation Award
PHP Programming Innovation award nominee
February 2019
Number 7
Many applications need to deal with the local time in different zones of the world.

Each time zone has an abbreviated name but humans understand better a more readable name.

This class can return the names of timezone either in short and full form, so applications can display the full name form in a user friendly manner.

Manuel Lemos
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 37 packages by
Country: United Kingdom United Kingdom
Age: ???
All time rank: 52726 in United Kingdom United Kingdom
Week rank: 24 Up1 in United Kingdom United Kingdom Up
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Details

World Date ???

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


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Image file screenshot-world-date.png Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file timezones.json Data Auxiliary data
  Plain text file WorldDate.php Class Renamed library

 Version Control Unique User Downloads Download Rankings  
 100%
Total:117
This week:1
All time:9,008
This week:345Up