📜 ⬆️ ⬇️

Dater - determines the time zone, localizes and formats the time in PHP



When developing a web project involves the task of accounting for users' time zones, each time the solution seems to have to be reinvented. Of the many projects known to me that solved this problem, I don’t recall a single solution that could be called simple, transparent for the whole architecture and universal.

But what if you just want a few lines of code, and so that the rest of the code never finds out that customer data should be given in a different time zone, and that the data received from the client can indicate time with a different time zone from the server one? To solve this and many other problems, today I will tell you about one concise and functional library for PHP.
')
And so, meet - Dater , and its main features:


The promised lines of code for automatically defining and processing a custom time zone will be left for dessert, but for now let's go over the main features of Dater, in brief, using examples.

Binding formats


Allows you to standardize for the project a set of used formats and work with them by name:

$dater = new Dater\Dater(new Dater\Locale\En()); $dater->format(time(), 'd/m/Y'); // 2013/03/14 $dater->addFormat('slashedDate', 'd/m/Y'); $dater->format(time(), 'slashedDate'); // 2013/03/14 $dater->slashedDate(time()); // 2013/03/14 

Expansion of formatting options


All formatting options from date () are available , which can also be redefined and extended:

 $dater->addFormatOption('ago', function (DateTime $datetime) { return floor((time() - $datetime->getTimestamp()) / 86400) . ' days ago'; }); $dater->format(time() - 60*60*24*7, 'd FY, ago'); // 14 March 2013, 7 days ago 

Locale support


 $dater->setLocale(new Dater\Locale\En()); echo $dater->date(); // 03/21/2013 echo $dater->now('j F Y'); // 21 March 2013 $dater->setLocale(Dater\Dater::getLocaleByCode('ru')); echo $dater->date(); // 21.03.2013 echo $dater->now('j F Y'); // 21  2013 

Standard methods for server and user formats based on locale


 echo $dater->date(); // 03/21/2013 (client timezone, depends on locale) echo $dater->time(); // 5:41 AM (client timezone, depends on locale) echo $dater->datetime(); // 03/21/2013 5:41 (client timezone, depends on locale) echo $dater->isoDate(); // 2013-03-21 (client timezone) echo $dater->isoTime(); // 05:41:28 (client timezone) echo $dater->isoDatetime(); // 2013-03-21 05:41:28 (client timezone) echo $dater->serverDate(); // 2013-03-21 (server timezone) echo $dater->serverTime(); // 09:41:28 (server timezone) echo $dater->serverDatetime(); // 2013-03-21 09:41:28 (server timezone) 

Date-time conversion based on time zone


 $dater->setServerTimezone('Europe/Moscow'); $dater->setClientTimezone('Europe/London'); echo $dater->serverDatetime(); // 2013-03-21 08:18:06 echo $dater->isoDatetime(); // 2013-03-21 04:18:06 echo $dater->time(); // 04:18 

It is worth mentioning that when calling $dater->setServerTimezone('Europe/Moscow'); The date () function and the DateTime class will return the time in the newly set time zone. To disable this, pass the false method to the second parameter.

Finally, the promised


A code that allows you to automatically determine the client's time zone and display the current date-time for it:

In the header of the global initialization script

 $dater = new Dater\Dater(new Dater\Locale\Ru(), 'Europe/Moscow'); $timezoneDetector = new Dater\TimezoneDetector(); $dater->setClientTimezone($timezoneDetector->getClientTimezone()); $dataHandler = new Dater\DataHandler($dater); $dataHandler->enableOutputTimezoneHandler(); $dataHandler->convertRequestDataToServerTimezone(); 

In the main template

 <html> <head> <?= $timezoneDetector->getHtmlJsCode() ?> </head> </html> 

Now all the lines YYYY-MM-DD HH: MM: SS in the sent data will be replaced by YYYY-MM-DD HH: MM: SS in the automatically determined client time zone. If you need to output the date-time in a certain format, then it is enough to add YYYY-MM-DD HH: MM: SS [H md] or YYYY-MM-DD HH: MM: SS [date] where date is zabindenny in Dater . You can also output and format the timestamp format: 1363853607 [dmY].

For example, the following data

 <html> <body> Timestamp format: 1363238564 ( ) Timestamp format: 1363238564[Y/m/d] Timestamp format: 1363238564[datetime] Server datetime format: 2013-03-14 09:22:44[Y/m/d] Server datetime format: 2013-03-14 09:22:44[time] Server datetime format: 2013-03-14 09:22:44 </body> </html> 

Will be automatically converted to

 <html> <body> Timestamp format: 1363238564 ( ) Timestamp format: 2013/03/14 Timestamp format: 14.03.2013 07:22 Server datetime format: 2013/03/14 Server datetime format: 07:22 Server datetime format: 2013-03-14 07:22:44 </body> </html> 


At the same time, $dataHandler->convertRequestDataToServerTimezone(); will make so that all YYYY-MM-DD HH: MM: SS data received from the client will be converted into YYYY-MM-DD HH: MM: SS server time zone. Thus, the server will never know that the client receives and sends the date-time in a different time zone.

We have to admit that this is a slightly extreme variant of processing time zones. A more universal and traditional solution would be to stop using $dataHandler->enableOutputTimezoneHandler(); and simply frame each date-time by calling the appropriate formatting method. For example, <?= $dater->date($datetimeOrTimestamp) ?> .

about the project


Honestly, I am the author of this library, and I will be very grateful for any criticism and help in the finalization. Sources are posted on GitHub under a free BSD license, use and distribute as you wish.

I hope someone still come in handy :)

UPD By numerous requests, the library was refactored using the namespace, in accordance with PSR-0. The previous version is saved in a separate brunch , and is available from Composer as "dater/dater":"1.*@dev" .

Source: https://habr.com/ru/post/173693/


All Articles