Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jeff Madsen:
What’s all this “immutable date” stuff, anyway?
Sep 06, 2017 @ 10:18:42

Jeff Madsen has a post on his Medium blog sharing some of his thoughts about immutable DateTime types, what the difference is between mutable and immutable and "why you should care".

I’m going to show you the difference between the two using two popular Php DateTime libraries? - Carbon and Chronos, and then demonstrate the danger of using the mutable one of those.

You have probably used Carbon? - ?it is a wonderful library put together by Brian Nesbitt that takes all the pain out of working with dates. It has got one “short-coming”, if you will?- ?it is built on top of the DateTime object.

He gives an example of why this is a problem with Carbon (mutable) and how it's handled differently in Chronos (immutable). He makes the point that, unless your date values are immutable, you don't have any idea of they've changed elsewhere in the processing. He gives a more real-world example of working with immutable objects with a "user" model class and the "name" properties attached to it.

tagged: immutable date carbon chronos bug example code tutorial

Link: https://medium.com/@codebyjeff/whats-all-this-immutable-date-stuff-anyway-72d4130af8ce

Ross Tuck:
Precision Through Imprecision: Improving Time Objects
Aug 10, 2017 @ 11:29:04

In a recent post to his site Ross Tuck looks at improving time objects with imprecision, basically determining the granularity you require and using that in all time operations.

When modeling important numbers, it’s considered good form to specify the precision. Whether it’s money, size or weight; you’ll typically round off to a given decimal point. Even if it’s only for user display, rounding off makes the data more predictable for manipulation and storage.

Unfortunately, we don’t often do this when handling time and it bites us in the rear.

He gives an example of a use of the DateTime handling in PHP and possible inconsistencies when working with the time portion. He offers three possible solutions: one where the time is specified correctly, making a relative modification on the object being checked and, his ultimate solution, just "throwing away" the time and not worrying about it. He then shows how to encapsulate this checking and a few other issues that can come with date/time and evaluations (like equals checks).

tagged: datetime imprecision time object date tutorial

Link: http://rosstuck.com/precision-through-imprecision-improving-time-objects

SitePoint PHP Blog:
Localizing Dates, Currency, and Numbers with Php-Intl
May 23, 2016 @ 12:52:32

On the SitePoint PHP blog Younes Rafie has continued his series about the PHP "Intl" extension for use in internationalizing an application. in this second part of the series he moves away from just strings and looks at using it for currencies and numbers.

The first part of this series was an introduction of the PHP Intl extension and of how to localize your application’s messages. In this part, we’re going to learn about localizing numbers, dates, calendars, and similar complex data.

The post is broken down into a few different sections, each with their own examples:

  • Localizing Decimals
  • Localizing Currencies
  • Timezones
  • Calendars

The "Intl" extension makes these operations relatively simple with plenty of built-in objects and methods to help with the translations between the formats. You can find out more about this extension in the PHP manual.

tagged: date currency localization number tutorial intl extension series part2

Link: https://www.sitepoint.com/localizing-dates-currency-and-numbers-with-php-intl/

SitePoint PHP Blog:
Suggesting Carbon with Composer – Date and Time the Right Way
Nov 16, 2015 @ 09:16:58

On the SitePoint PHP blog there's a tutorial that's been posted spotlightling a PHP library that can make working with dates and times simpler: Carbon . In this new tutorial they walk you through what the library has to offer and plenty of examples of it in use.

Carbon is a small library for date and time manipulation in PHP. It relies on and extends the core DateTime class, adding helpful methods for a significantly saner experience.

In this article, we’ll take a look at some basic usage examples, and then use it in a real project.

They start with the installation (via Composer) and a few examples of it in use, determining if a given date string is a weekend, in a leap year, etc. They also talk about localization support and working with time intervals. He then gets into the more real-world part of the example, updating the Diffbot client to optionally support Carbon for its date/time handling. He starts with some tests to define how he wants the handling to work and how to use it to parse the date returned from the DiffBot API.

tagged: carbon datetime library tutorial date time diffbot client api

Link: http://www.sitepoint.com/suggesting-carbon-with-composer-date-and-time-the-right-way/

Rob Allen:
Overriding the built-in Twig date filter
Dec 16, 2014 @ 09:45:31

In his latest post Rob Allen shows a way you can override the default Twig date filter with your own custom Date extension handling.

In one project that I'm working on, I'm using Twig and needed to format a date received from an API. The date string received is of the style "YYYYMMDD", however date produced an unexpected output. [...] This surprised me. Then I thought about it some more and realised that the date filter is treating my date string as a unix timestamp. I investigated and discovered the problem in twig_date_converter.

He includes some example code you'll need to create the custom renderer. As part of the internals of how Twig formats the date currently is internal and can't be changed, he opted to override the extension itself. As a result, the call to the filter is exactly the same as before, the output results are just formatted more correctly.

tagged: twig override default date filter custom extension

Link: http://akrabat.com/php/overriding-the-built-in-twig-date-filter/

Ben Ramsey:
Dates Are Hard
Feb 24, 2014 @ 09:03:24

In a new post to his site Ben Ramsey talks about why dates are hard and can be frustrating to work with sometimes. It revolves around an issue he recently found with calculating a time for a UUID and days of the week.

No, I’m not talking about a meeting with a lover or potential lover. While those can be stressful, the calendar math used to determine the precise date and time on which such a meeting might occur is infinitely more difficult to perform. To software programmers, this isn’t news, but I recently encountered an issue when calculating the time for an RFC 4122 UUID that had me questioning the accuracy of our modern, accepted calendars, especially with regard to the days of the week on which our dates fall.

In his work on his UUID library, he came across a the bug because of some failing unit tests. It was only happening in certain versions of PHP and upon further investigation found the issue to be a wrong day of the week from a date in 1582 (the correct value being Sunday). As it turned out, the date in question was actually a Saturday and his local environment was reporting bad results. The problem was with a revision made to the Gregorian calendar, removing 10 days causing a difference between the Gregorian and Great Britain versions of 1582.

tagged: date time calendar gregorian unittest greatbriain year

Link: http://benramsey.com/blog/2014/02/dates-are-hard/

Dates and Time - The OOP Way
Oct 24, 2013 @ 11:49:23

On NetTuts.com today there's a new tutorial they've posted showing how to use PHP's DateTime functionality in a more OOP kind of way. The DateTime extension lets you work both ways - procedural and OOP, with only slightly different syntax changes between them.

The Date/Time PHP extension is a set of classes that allow you to work with almost all of the date and time related tasks. It’s been available since the release of PHP 5.2 and the extension introduced several new classes.

The tutorial first shows you some of the differences between just working with something like date and DateTime. From there they get into a bit more complicated things like:

  • Modifying dates/times
  • Working with multiple dates
  • Working with timezones
  • Using DatePeriods
  • Extending the current functionality

There's also two more "real world" usage scenarios included - defaulting to using UTC times and using the DateInterval to handle subscription payment logic.

tagged: datetime tutorial oop introduction date time

Link: http://net.tutsplus.com/tutorials/php/dates-and-time-the-oop-way/

Derick Rethans:
ISO 8601 week dates
Sep 24, 2013 @ 09:54:31

Derick Rethans has a new post with details about handling ISO 8601 dates in PHP via the DateTime functionality. It's a response to some bugs filed having to do with week numbering.

Week numbers are defined in this same ISO 8601 standard. Each year has 52 or 53 weeks and weeks always start on a Monday. Week number 1 of each year is the first week in a year that has the first Thursday of the year, or in other words, the week containing January 4th.

He talks about some of the date format arguments that use would use when working with the ISO 8601 formatting and how it relates to the calendar year. He points out that the "Y" format specifier is not the same as the "o" - the first being the calendar year while the second relates to the ISO 8601 year.

As conclusion, this article shows that there are two ways representing dates in PHP. In the Gregorian1 calendar with year, month and day (of month), and in the ISO 8601 calendar with year, week and day (of week). The format characters for the two different years are either Y or o and they should not be confused.
tagged: iso8601 date datetime format specifier gregorian calendar difference

Link: http://derickrethans.nl/iso-8601-weeks.html

Lately in PHP, Episode 36 - PHP 5.5 Release Date Imminent
Jun 17, 2013 @ 09:30:38

On the PHPClasses.org site Manuel Lemos has posted the latest episode of the "Lately in PHP" podcast series. This episode, number 36, talks about the upcoming release of PHP 5.5 and looks forward to 5.6.

The PHP 5.5.0 final release is about to happen. After about 16 months of development PHP 5.5 is bringing even more maturity to the PHP language, which by Google numbers is present in 75% of the Web sites. [...] hey also discussed some new features proposed for PHP 5.6 like incremental decoding of large JSON data streams and overloading arithmetic operators for arbitrary precision math. They also debated the meaning of Google finally adding support to PHP in their cloud hosting platform Google AppEngine. Also in this episode it was discussed an article about good practices to quickly detect and fix PHP code bugs that only show up in production environments.

You can listen to this latest episode either through the in-page player, by downloading it directly or you can watch the video of the live recording on YouTube.

tagged: release date latelyinphp podcast series ep36 appengine

Link: http://www.phpclasses.org/blog/post/209-PHP-55-Release-Date-Imminent--Lately-in-PHP-podcast-episode-36.html

Lately in PHP, Episode 28 - PHP 5.5 Release Date and Plans
Oct 15, 2012 @ 08:40:06

On the PHPClasses.org blog they've posted the latest "Lately In PHP" Podcast - Episode #28, "PHP 5.5 Release Date and Plans":

PHP 5.5 release is already being planned. The release manager was picked, the new features are decided and the release date is estimated. This one of the main topics discussed by Manuel Lemos and Ernani Joppert in episode 28 of the Lately in PHP podcast. They also discussed the interpretation from VentureBeat that Zend is working on mobile version of PHP, using abstract symbol tables to optimize PHP further, and creating classes that provide APIs that are simpler for the users.

You can listen to this latest episode either using the in-page player, by downloading the mp3 or by subscribing to their feed.

tagged: release date latelyinphp podcast phpclasses language