CSS Inheritance, Datetime Conversions in PHP

February 20, 2016
Category: TIL
Tags: CSS and PHP

Today I learned:

CSS Inheritance

  • In CSS some styles are automatically inherited from their ancestors (i.e. elements which contain that element). For example, if you set body {color: red;}, all text inside the body element, regardless of what other elements the text is in, will be red unless the color style is set on one of those other elements later in the .css file.
  • This simultaneously makes writing CSS a breeze and debugging it a bit of a headache. There isn’t a great way to tell if a style is inheritable without looking at the docs.
  • If you encounter an issue where your style isn’t displaying as it should and it is not responding to changes, check the docs to see if the style can be automatically inherited. If so, check the ancestors of the element you are working on.

The issue I ran into today was that my <pre> snippets kept wrapping no matter what overflow and width were set to. It turns out that word-wrap is automatically inherited and I had it set on an ancestor element.

Date/time conversions in PHP

Date conversions are tricky. Thankfully, PHP has some built-in functions to make this easier.

Example: date("c", time()) will take the current time (provided as a Unix timestamp) and convert it to ISO 8601 format.

Unix timestamps are the number of seconds that have elapsed since the Unix Epoch (00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970). They can be frustrating at first because they are so foreign looking, but they are really useful because:

  1. They are not subject to timezones.
  2. Since they are simply seconds, time math with Unix timestamps is pretty simple: Convert the time you wand to add or subtract into seconds and do the direct calculation. Then if you want to get it back into a recognizable format, use one of the functions above.

I’ve been working on a custom Slack command to input time into Toggl, so I’ve been doing quite a bit with time conversions to get everything right.

Find this post useful?

Buy me a coffeeBuy me a coffee