fewagency/carbonator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fewagency/carbonator

Composer 安装命令:

composer require fewagency/carbonator

包简介

Datetime helpers for timezone handling

README 文档

README

Build Status

A collection of datetime helpers built on Carbon. They help you easily parse or convert strings or DateTimes between timezones (and format them) with one single call.

A good pattern is to always keep your app working with times in the UTC timezone internally and to store any time data in that default timezone. If you're temped to or already use another timezone as default, please read the following article: Always Use UTC Dates And Times.

This is how Carbonator can help you juggle those timezones:

  1. Actively convert any times to UTC when user data enters the application. This is the perfect job for Carbonator::parseToDefaultTz($input, $tz_parse = null).
  2. If needed, convert to the user's timezone just before display or when populating inputs. parseToDatetimeLocal($input, $tz_target = null, $tz_parse = null) is one of the methods that can be used to directly format a string for display.

Examples

// Create DateTime in any timezone and get it in your app's default timezone:
$in_sweden = Carbon::parse('2016-08-07 13:37', 'Europe/Stockholm');
// Stockholm is 2 hours ahead of UTC during daylight savings time
$in_utc = Carbonator::parseToDefaultTz($in_sweden);
echo $in_utc->toCookieString();
// Sunday, 07-Aug-2016 11:37:00 UTC


// Parse directly from a string in a timezone and get it in your app's default timezone:
$in_utc = Carbonator::parseToDefaultTz('2016-08-07 13:37', '-05:00');
echo $in_utc->toCookieString();
// Sunday, 07-Aug-2016 18:37:00 UTC


// Format a time for a user in Namibia
$in_utc = Carbon::parse('2016-08-07 13:37');
// Windhoek is 1 hour ahead of UTC when not on daylight savings time (WAT: West Africa Time)
echo Carbonator::formatInTz($in_utc, 'D, M j, Y H:i T', 'Africa/Windhoek');
// Sun, Aug 7, 2016 14:37 WAT


// Populate a HTML5 datetime-local input for a user in Japan:
$in_utc = Carbon::parse('2016-08-07 13:37');
echo Carbonator::parseToDatetimeLocal($in_utc, 'Asia/Tokyo');
// 2016-08-07 22:37


// Populate a HTML5 datetime attribute
$in_india = Carbon::parse('2016-08-07 13:37', 'Asia/Kolkata');
echo Carbonator::parseToDatetime($in_india);
// 2016-08-07T13:37:00+05:30

Installation & Configuration

composer require fewagency/carbonator

Background

The Carbon package is excellent, but it doesn't support parsing other DateTime instances while keeping their timezone. That's just the behaviour of the PHP DateTime constructor and not much to do about.

Also, Carbon throws exceptions on errors. To be able to parse any user input without having to filter or validate it first, I wanted wrappers that just returns null when input can't be parsed.

I think it makes sense putting these opinionated parsing and display methods into a separate helper package instead of having Carbon do even more work.

Usage

The first parameter - $input

All of Carbonator's methods take a string or PHP DateTime instance as the first $input parameter.

This $input will be turned into a Carbon instance to process. Remember that Carbon extends DateTime and can always be used safely as input.

Any empty $input or any errors in parsing will make methods return null.

The last parameter - $tz_parse

All of Carbonator's methods also take the $tz_parse timezone as the last parameter. The $tz_parse timezone will be used as context when parsing an $input string, but will be ignored when parsing DateTime instances, as they already have a timezone. As per default DateTime behaviour, $tz_parse will also be ignored whenever there is timezone information supplied within the string.

Timezone parameters

Timezones can be supplied in string format (e.g. 'Europe/Stockholm' or '+01:00') or as a PHP DateTimeZone instance.

Remember, named timezones like Europe/Stockholm takes daylight savings time (summer time) into account, whereas offset timezones like +01:00 does not. Use named timezones if you can, your users will appreciate it.

If a timezone parameter is left out or null when calling a method, the PHP default timezone from date_default_timezone_get() will be used.

Hopefully your default timezone will be UTC. Some frameworks, like Laravel, set the timezone explicitly using a config value and you should keep that as UTC unless you really know what you're trying to achieve with another setting. Read more below in Check your setup.

Methods

Carbonator::parse($input, $tz_parse = null)

Returns a Carbon instance. Note that the timezone of the output doesn't always match the supplied context timezone from the 2nd parameter. The output is just guaranteed to be Carbon or null.

Carbonator::parseToTz($input, $tz_target = null, $tz_parse = null)

Returns a Carbon instance, guaranteed to be in the $tz_target timezone.

Carbonator::parseToDefaultTz($input, $tz_parse = null)

Returns a Carbon instance, guaranteed to be in the PHP default timezone.

Carbonator::formatInTz($input, $format, $tz_target = null, $tz_parse = null)

Returns a string formatted according to the $format - see docs for PHP's date(). The string is guaranteed to be in the $tz_target timezone.

Carbonator::parseToDatetimeLocal($input, $tz_target = null, $tz_parse = null)

Returns a string formatted for a HTML5 datetime-local <input/> element in the 'Y-m-d H:i' format (e.g. '2016-08-07 13:37'), with no timezone information. Be kind to your users and display the timezone next to your input, perhaps in the <label> or in an element referenced through the input's aria-describedby attribute.

Carbonator::parseToDatetime($input, $tz_target = null, $tz_parse = null)

Returns a string formatted in the "W3C" format 'Y-m-d\TH:i:sP' (e.g. '2016-08-07T13:37:00+00:00'), suitable for a HTML5 <time/> element's datetime attribute.

Figuring out users' timezones

There's no way to know a user's timezone for sure, without asking them. If you want to help your users select a timezone, PHP's timezone_identifiers_list() is a nice tool to find relevant timezones by country or continent.

The list is quite long though, so you could do IP-location on the server side to suggest a timezone to the user. For Laravel there's a package called Laravel Geoip doing just that by connecting to different services.

In the browser you could run some javascript, for example jstimezonedetect to guess the named timezone of your user from the timezone offset reported by their operating system. Or you could use the browser's Geolocation API, but that could be seen as a bit intrusive by the user, with permissions and everything.

In Laravel, there's a handy validation rule for timezone identifiers.

Check your setup

This console command will check your Linux system timezone:

date +%Z

Hopefully it's UTC.

This query can be run by MySQL to check which timezones your database is using:

SELECT @@global.time_zone, @@session.time_zone

Hopefully both display as SYSTEM or UTC.

From within PHP you can use date_default_timezone_get() to check what default timezone is used. If it's not UTC, find out where it's set by either:

In Laravel

If you're using Laravel, you set your app's default timezone in your app's config/app.php.

You can easily check a database-connection's timezone using the artisan tinker command from the terminal:

php artisan tinker

Then run:

DB::select('SELECT @@global.time_zone, @@session.time_zone');

Again, hopefully both are reported as SYSTEM or UTC. If the session timezone is wrong, an optional 'timezone' parameter can be set on MySQL connections in your app's config/databases.php. Don’t set it manually unless you need to, in which case it should be the same as the Laravel config, i.e 'UTC'. Actually, pulling in config('app.timezone') in the database config file works too!

Other methods

Here's a good read on Getting MySQL To Do It when it comes to timezones and PHP.

Authors

I, Björn Nilsved, created this package while working at FEW.

fewagency/carbonator 适用场景与选型建议

fewagency/carbonator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.99k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helper」 「timezone」 「datetime」 「carbon」 「datetime-local」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fewagency/carbonator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fewagency/carbonator 我们能提供哪些服务?
定制开发 / 二次开发

基于 fewagency/carbonator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-06